日期:2014-05-16  浏览次数:20495 次

PHP功能集合类源码

代码如下:

<?php
/**  
* 常用工具类  
* author Lee.  艾妮 http://ini.iteye.com
* Last modify $Date: 2012-8-23
*/
class Tool {
	/**
	 * js 弹窗并且跳转
	 * @param string $_info
	 * @param string $_url
	 * @return js
	 */
	static public function alertLocation($_info, $_url) {
		echo "<script type='text/javascript'>alert('$_info');location.href='$_url';</script>";
		exit();
	}
	
	/**
	 * js 弹窗返回
	 * @param string $_info
	 * @return js
	 */
	static public function alertBack($_info) {
		echo "<script type='text/javascript'>alert('$_info');history.back();</script>";
		exit();
	}
	
	/**
	 * 页面跳转
	 * @param string $url
	 * @return js
	 */
	static public function headerUrl($url) {
		echo "<script type='text/javascript'>location.href='{$url}';</script>";
		exit();
	}
	
	/**
	 * 弹窗关闭
	 * @param string $_info
	 * @return js
	 */
	static public function alertClose($_info) {
		echo "<script type='text/javascript'>alert('$_info');close();</script>";
		exit();
	}
	
	/**
	 * 弹窗
	 * @param string $_info
	 * @return js
	 */
	static public function alert($_info) {
		echo "<script type='text/javascript'>alert('$_info');</script>";
		exit();
	}
	
	/**
	 * 系统基本参数上传图片专用
	 * @param string $_path
	 * @return null
	 */
	static public function sysUploadImg($_path) {
		echo '<script type="text/javascript">document.getElementById("logo").value="'.$_path.'";</script>';
		echo '<script type="text/javascript">document.getElementById("pic").src="'.$_path.'";</script>';
		echo '<script type="text/javascript">$("#loginpop1").hide();</script>';
		echo '<script type="text/javascript">$("#bgloginpop2").hide();</script>';
	}
	
	/**
	 * html过滤
	 * @param array|object $_date
	 * @return string
	 */
	static public function htmlString($_date) {
		if (is_array($_date)) {
			foreach ($_date as $_key=>$_value) {
				$_string[$_key] = Tool::htmlString($_value);  //递归
			}
		} elseif (is_object($_date)) {
			foreach ($_date as $_key=>$_value) {
				$_string->$_key = Tool::htmlString($_value);  //递归
			}
		} else {
			$_string = htmlspecialchars($_date);
		}
		return $_string;
	}
	
	/**
	 * 数据库输入过滤
	 * @param string $_data
	 * @return string
	 */
	static public function mysqlString($_data) {
		$_data = trim($_data);
		return !GPC ? addcslashes($_data) : $_data;
	}
	
	/**
	 * 清理session
	 */
	static public function unSession() {
		if (session_start()) {
			session_destroy();
		}
	}
	
	/**
	 * 验证是否为空
	 * @param string $str
	 * @param string $name
	 * @return bool (true or false)
	 */
	static function validateEmpty($str, $name) {
		if (empty($str)) {
			self::alertBack('警告:' .$name . '不能为空!');
		}
	}
	
	/**
	 * 验证是否相同
	 * @param string $str1
	 * @param string $str2
	 * @param string $alert
	 * @return JS 
	 */
	static function validateAll($str1, $str2, $alert) {
		if ($str1 != $str2) self::alertBack('警告:' .$alert);
	}
	
	/**
	 * 验证ID
	 * @param Number $id
	 * @return JS
	 */
	static function validateId($id) {
		if (empty($id) || !is_numeric($id)) self::alertBack('警告:参数错误!');
	}
	
	/**
	 * 格式化字符串
	 * @param string $str
	 * @return string
	 */
	static public function formatStr($str) {
		$arr = array(' ', '	', '&', '@', '#', '%',  '\'', '"', '\\', '/', '.', ',', '$', '^', '*', '(', ')', '[', ']', '{', '}', '|', '~', '`', '?', '!', ';', ':', '-', '_', '+', '=');
		foreach ($arr as $v) {
			$str = str_replace($v, '', $str);
		}
		return $str;
	}
	
	/**
	 * 格式化时间
	 * @param int $time 时间戳
	 * @return string
	 */
	static public function formatDate($time='default') {
		$date = $time == 'default' ? date('Y-m-d H:i:s', time()) : date('Y-m-d H:i:s', $time);
		return $date;
	}
	
	/**  
	* 获得真实IP地址  
	* @return string  
	*/
	static public function realIp() {   
	    static $realip = NULL;   
	    if ($realip !== NULL) return $realip;  
	    if (isset($_SERVER)) {