XML文档对象转化为数组 XmlToArray

发布于:2013-12-25 17:57 作者:admin 浏览:1622 分类:PHP集合
/**
 * 获取XML文档对象转化为数组
 * @param simplexml $obj XML文档对象
 * @return Array 节点的数据
 */
function XmlToArray($obj){
	if(is_object($obj)){
		$obj=get_object_vars($obj);
	}
	
	if(is_array($obj)){
		foreach ($obj as $key=>$value){
			$obj[$key] = XmlToArray($value);
		}
	}
	return $obj;
}

0

字符串截取csubstr

发布于:2013-12-25 17:37 作者:admin 浏览:2006 分类:PHP集合
/*   
   * @支持gb2312,gbk,utf-8,big5 字符串截取
   * @param string $str 要截取的字串   
  * @param int $start 截取起始位置   
  * @param int $length 截取长度   
  * @param string $charset utf-8|gb2312|gbk|big5 编码    
  * @param $suffix 是否加尾缀   
  */    


function csubstr($str, $start=0, $length, $charset="utf-8", $suffix=true)   
{   
 if(function_exists("mb_substr"))   
  return mb_substr($str, $start, $length, $charset);   
 $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";    
 $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";   
 $re['gbk']   = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";   
 $re['big5']   = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";    
 preg_match_all($re[$charset], $str, $match);   
 $slice = join("",array_slice($match[0], $start, $length));   
 if($suffix) return $slice."…";   
 return $slice;   
} 

0

转义定符串函数yaddslashes

发布于:2013-12-25 17:35 作者:admin 浏览:1580 分类:PHP集合
/**
 * @name yaddslashes
 * @desc 转义定符串函数
 * @param string $string
 * @return mixed
 */
function yaddslashes($string)
{
	if(!get_magic_quotes_gpc())
	{
		if(is_array($string)) {
			foreach($string as $key => $val) {
				$string[$key] = yaddslashes($val);
			}
		} else {
			$string = addslashes($string);
		}
	}
	return $string;
}

0

获得客户端ip

发布于:2013-12-25 17:33 作者:admin 浏览:1548 分类:PHP集合
//获得客户端ip


function getClientIp()
{
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$onlineip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$onlineip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$onlineip = $_SERVER['REMOTE_ADDR'];
	}
	return $onlineip;
}

0