微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

一些php项目中比较通用的php自建函数的详解

以下一些PHP函数是我们it动力最常用的项目开发函数,这些函数还算是在比较多的项目中使用到的,也是比较通用的。

<FONT style="COLOR: #ff0000">1.请求接口的处理函数

<div class="codetitle"><a style="CURSOR: pointer" data="93468" class="copybut" id="copybut93468" onclick="doCopy('code93468')"> 代码如下:
<div class="codebody" id="code93468">
/
curl访问程序接口
@param string
@return array
/
function getCurlDate($url,$datas,$key) {
$datas['time'] = $_SERVER['REQUEST_TIME'] + 300;
$post_data['post'] = urlencode(authcode(serialize($datas),"ENCODE",$key));
// echo $url;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
// 我们在POST数据哦!
curl_setopt($ch,CURLOPT_POST,1);
// 把post的变量加上
curl_setopt($ch,CURLOPT_POSTFIELDS,$post_data);
$output = curl_exec($ch);
// dump(curl_error($ch));
curl_close($ch);
return json_decode($output,true);
}

<FONT style="COLOR: #ff0000">2.获取文件扩展名

<div class="codetitle"><a style="CURSOR: pointer" data="85182" class="copybut" id="copybut85182" onclick="doCopy('code85182')"> 代码如下:
<div class="codebody" id="code85182">
/

@获取文件扩展名
@$pic string 图片路径
*/
function get_file_ext($pic) {
return substr($pic,strrpos($pic,'.') + 1);
}

<FONT style="COLOR: #ff0000">3.可逆的加密、解密函数

<div class="codetitle"><a style="CURSOR: pointer" data="63156" class="copybut" id="copybut63156" onclick="doCopy('code63156')"> 代码如下:
<div class="codebody" id="code63156">
/
字符串加密
@param $string 需加密的字符
@param $operation 加密或解密
@param $key 网站加密key,防止破解
@return string
/
function authcode($string,$operation = 'DECODE',$key = '',$expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : '^www.itokit.com$');
$keya = md5(substr($key,16));
$keyb = md5(substr($key,16,16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string,$ckey_length) : substr(md5(microtime()),-$ckey_length)) : ''; $cryptkey = $keya . md5($keya . $keyc);
$key_length = strlen($cryptkey); $string = $operation == 'DECODE' ? base64_decode(substr($string,$ckey_length)) : sprintf('%010d',$expiry ? $expiry + time() : 0) . substr(md5($string . $keyb),16) . $string;
$string_length = strlen($string); $result = '';
$Box = range(0,255); $rndkey = array();
for ($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
} for ($j = $i = 0; $i < 256; $i++) {
$j = ($j + $Box[$i] + $rndkey[$i]) % 256;
$tmp = $Box[$i];
$Box[$i] = $Box[$j];
$Box[$j] = $tmp;
} for ($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $Box[$a]) % 256;
$tmp = $Box[$a];
$Box[$a] = $Box[$j];
$Box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($Box[($Box[$a] + $Box[$j]) % 256]));
} if ($operation == 'DECODE') {
if ((substr($result,10) == 0 || substr($result,10) - time() > 0) && substr($result,10,16) == substr(md5(substr($result,26) . $keyb),16)) {
return substr($result,26);
} else {
return '';
}
} else {
return $keyc . str_replace('=','',base64_encode($result));
}
}

<FONT style="COLOR: #ff0000">4.字符串转十六进制

<div class="codetitle"><a style="CURSOR: pointer" data="2677" class="copybut" id="copybut2677" onclick="doCopy('code2677')"> 代码如下:
<div class="codebody" id="code2677">
/

字符串转十六进制
@param unkNown_type $s
*/
function str2hex($s) {
$r = "";
$hexes = array ("0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f");
for ($i=0; $i<strlen($s); $i++)
$r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
return $r;
}

<FONT style="COLOR: #ff0000">5.十六进制转字符串

<div class="codetitle"><a style="CURSOR: pointer" data="84114" class="copybut" id="copybut84114" onclick="doCopy('code84114')"> 代码如下:<div class="codebody" id="code84114">
/
十六进制转字符串
@param unkNown_type $s
*/
function hex2str($s) {
$r = "";
for ( $i = 0; $i<strlen($s); $i += 2)
{
$x1 = ord($s{$i});
$x1 = ($x1>=48 && $x1<58) ? $x1-48 : $x1-97+10;
$x2 = ord($s{$i+1});
$x2 = ($x2>=48 && $x2<58) ? $x2-48 : $x2-97+10;
$r .= chr((($x1 << 4) & 0xf0) | ($x2 & 0x0f));
}
return $r;
}

<FONT style="COLOR: #ff0000">6.返回经addslashes处理过的字符串或数组

<div class="codetitle"><a style="CURSOR: pointer" data="66361" class="copybut" id="copybut66361" onclick="doCopy('code66361')"> 代码如下:<div class="codebody" id="code66361">
/
返回经addslashes处理过的字符串或数组
@param $string 需要处理的字符串或数组
@return mixed
/
function new_addslashes($string){
if(!is_array($string)) return addslashes($string);
foreach($string as $key => $val) $string[$key] = new_addslashes($val);
return $string;
} /
/
function addslashes_deep($string)
{
return is_array($string) ? array_map('addslashes_deep',$string) : addslashes($string);
}

<FONT style="COLOR: #ff0000">7.返回经stripslashes处理过的字符串或数组

<div class="codetitle"><a style="CURSOR: pointer" data="93064" class="copybut" id="copybut93064" onclick="doCopy('code93064')"> 代码如下:<div class="codebody" id="code93064">
/
返回经stripslashes处理过的字符串或数组
@param $string 需要处理的字符串或数组
@return mixed
/
function new_stripslashes($string) {
if(!is_array($string)) return stripslashes($string);
foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
return $string;
}
/
/
function stripslashes_deep($string)
{
return is_array($string) ? array_map('stripslashes_deep',$string) : stripslashes($string);
}

<FONT style="COLOR: #ff0000">8.返回经 htmlspecialchars 处理过的字符串或数组

<div class="codetitle"><a style="CURSOR: pointer" data="12954" class="copybut" id="copybut12954" onclick="doCopy('code12954')"> 代码如下:<div class="codebody" id="code12954">
/
返回经 htmlspecialchars 处理过的字符串或数组
@param $string 需要处理的字符串或数组
@return mixed
/
function new_html_special_chars($string) {
if(!is_array($string)) return htmlspecialchars($string);
foreach($string as $key => $val) $string[$key] = new_html_special_chars($val);
return $string;
}

<FONT style="COLOR: #ff0000">9.获取请求ip

<div class="codetitle"><a style="CURSOR: pointer" data="79842" class="copybut" id="copybut79842" onclick="doCopy('code79842')"> 代码如下:<div class="codebody" id="code79842">
/

获取请求ip

@return ip地址
/
function ip() {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'),'unkNown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'),'unkNown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'),'unkNown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],'unkNown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d.]{7,15}/',$ip,$matches ) ? $matches [0] : '';
}

<FONT style="COLOR: #ff0000">10.字符截取 支持UTF8/GBK

<div class="codetitle"><a style="CURSOR: pointer" data="79178" class="copybut" id="copybut79178" onclick="doCopy('code79178')"> 代码如下:<div class="codebody" id="code79178">
/*
字符截取 支持UTF8/GBK
@param $string
@param $length
@param $dot
/
function str_cut($string,$length,$dot = '...') {
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array(' ',' ','&','"',''','“','”','—','<','>','·','…'),array('∵',"'",$string);
$strcut = '';
if(strtolower(CHARSET) == 'utf-8') {
$length = intval($length-strlen($dot)-$length/3);
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string,$n);
$strcut = str_replace(array('∵',array(' ',$strcut);
} else {
$dotlen = strlen($dot);
$maxi = $length - $dotlen - 1;
$current_str = '';
$search_arr = array('&','…','∵');
$replace_arr = array('&',' ');
$search_flip = array_flip($search_arr);
for ($i = 0; $i < $maxi; $i++) {
$current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
if (in_array($current_str,$search_arr)) {
$key = $search_flip[$current_str];
$current_str = str_replace($search_arr[$key],$replace_arr[$key],$current_str);
}
$strcut .= $current_str;
}
}
return $strcut.$dot;
}

<FONT style="COLOR: #ff0000">11.产生随机字符串

<div class="codetitle"><a style="CURSOR: pointer" data="54611" class="copybut" id="copybut54611" onclick="doCopy('code54611')"> 代码如下:<div class="codebody" id="code54611">
/

产生随机字符串

@param int $length 输出长度
@param string $chars 可选的 ,认为 0123456789
@return string 字符串
/
function random($length,$chars = '0123456789') {
$hash = '';
$max = strlen($chars) - 1;
for($i = 0; $i < $length; $i++) {
$hash .= $chars[mt_rand(0,$max)];
}
return $hash;
}

<FONT style="COLOR: #ff0000">12.将字符串转换为数组

<div class="codetitle"><a style="CURSOR: pointer" data="28312" class="copybut" id="copybut28312" onclick="doCopy('code28312')"> 代码如下:<div class="codebody" id="code28312">
/*
字符串转换为数组

@param string $data 字符串
@return array 返回数组格式,如果,data为空,则返回空数组
/
function string2array($data) {
if($data == '') return array();
eval("\$array = $data;");
return $array;
}

<FONT style="COLOR: #ff0000">13.将数组转换为字符串

<div class="codetitle"><a style="CURSOR: pointer" data="80651" class="copybut" id="copybut80651" onclick="doCopy('code80651')"> 代码如下:<div class="codebody" id="code80651">
/
将数组转换为字符串

@param array $data 数组
@param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,认为1
@return string 返回字符串,如果,data为空,则返回空
/
function array2string($data,$isformdata = 1) {
if($data == '') return '';
if($isformdata) $data = new_stripslashes($data);
return addslashes(var_export($data,TRUE));
}

<FONT style="COLOR: #ff0000">14.转换字节数为其他单位

<div class="codetitle"><a style="CURSOR: pointer" data="23635" class="copybut" id="copybut23635" onclick="doCopy('code23635')"> 代码如下:<div class="codebody" id="code23635">
/

转换字节数为其他单位


@param string $filesize 字节大小
@return string 返回大小
/
function sizecount($filesize) {
if ($filesize >= 1073741824) {
$filesize = round($filesize / 1073741824 100) / 100 .' GB';
} elseif ($filesize >= 1048576) {
$filesize = round($filesize / 1048576
100) / 100 .' MB';
} elseif($filesize >= 1024) {
$filesize = round($filesize / 1024 * 100) / 100 . ' KB';
} else {
$filesize = $filesize.' Bytes';
}
return $filesize;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐