PHP封装curl的调用接口及常用函数详解
如下所示:
=$length){ $next_str="…"; }else{ $next_str=""; } if(function_exists("mb_substr")){ if($suffix) //returnmb_substr($str,$start,$length,$charset)."…"; returnmb_substr($str,$start,$length,$charset).$next_str; else returnmb_substr($str,$start,$length,$charset); }elseif(function_exists('iconv_substr')){ if($suffix) //returniconv_substr($str,$start,$length,$charset)."…"; returniconv_substr($str,$start,$length,$charset).$next_str; else returniconv_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."…"; if(!$suffix)return$slice; return$slice; } /** *用户名*隐藏 *$str="如来神掌"; *echocut_str($str,1,0).'**'.cut_str($str,1,-1); *输出:如**掌 */ functioncut_str($string,$sublen,$start=0,$code='UTF-8') { if($code=='UTF-8') { $pa="/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/"; preg_match_all($pa,$string,$t_string); if(count($t_string[0])-$start>$sublen)returnjoin('',array_slice($t_string[0],$start,$sublen)); returnjoin('',array_slice($t_string[0],$start,$sublen)); } else { $start=$start*2; $sublen=$sublen*2; $strlen=strlen($string); $tmpstr=''; for($i=0;$i<$strlen;$i++) { if($i>=$start&&$i<($start+$sublen)) { if(ord(substr($string,$i,1))>129) { $tmpstr.=substr($string,$i,2); } else { $tmpstr.=substr($string,$i,1); } } if(ord(substr($string,$i,1))>129)$i++; } //if(strlen($tmpstr)<$strlen)$tmpstr.="..."; return$tmpstr; } } /* *解决json_encode转换中文的问题,在php5.4下json_encode本身解决的这个问题 */ functionunescapedUnicode($match) { returnmb_convert_encoding(pack('H*',$match[1]),'UTF-8','UCS-2BE'); } /** *截取手机号 *demo:13112345678 *return:131****5678 */ functionhidtel($phone){ $IsWhat=preg_match('/(0[0-9]{2,3}[-]?[2-9][0-9]{6,7}[-]?[0-9]?)/i',$phone);//固定电话 if($IsWhat==1){ returnpreg_replace('/(0[0-9]{2,3}[-]?[2-9])[0-9]{3,4}([0-9]{3}[-]?[0-9]?)/i','$1****$2',$phone); }else{ returnpreg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone); } } //获取IP functiongetIp() { if(isset($_SERVER)){ if(isset($_SERVER["HTTP_X_FORWARDED_FOR"])){ $realip=$_SERVER["HTTP_X_FORWARDED_FOR"]; }elseif(isset($_SERVER["HTTP_CLIENT_IP"])){ $realip=$_SERVER["HTTP_CLIENT_IP"]; }else{ $realip=$_SERVER["REMOTE_ADDR"]; } }else{ if(getenv("HTTP_X_FORWARDED_FOR")){ $realip=getenv("HTTP_X_FORWARDED_FOR"); }elseif(getenv("HTTP_CLIENT_IP")){ $realip=getenv("HTTP_CLIENT_IP"); }else{ $realip=getenv("REMOTE_ADDR"); } } return$realip; } //把汉字转换为数字 functiongetUnicodeFromOneUTF8($word) { //获取其字符的内部数组表示,所以本文件应用utf-8编码! //$word=iconv("gb2312","utf-8",$word); if(is_array($word)) $arr=$word; else $arr=str_split($word); //此时,$arr应类似array(228,189,160) //定义一个空字符串存储 $bin_str=''; //转成数字再转成二进制字符串,最后联合起来。 foreach($arras$value) $bin_str.=decbin(ord($value)); //此时,$bin_str应类似111001001011110110100000 //正则截取 $bin_str=preg_replace('/^.{4}(.{4}).{2}(.{6}).{2}(.{6})$/','$1$2$3',$bin_str); //此时,$bin_str应类似0100111101100000,如果是汉字"你" returnbindec($bin_str); //返回类似20320,汉字"你" //returndechex(bindec($bin_str)); //如想返回十六进制4f60,用这句
以上这篇PHP封装curl的调用接口及常用函数详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。