Home >php教程 >php手册 >一些php项目中比较通用的php自建函数的详解

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

WBOY
WBOYOriginal
2016-06-06 20:31:091174browse

本篇文章是对一些php项目中比较通用的php自建函数进行了详细的分析介绍,需要的朋友参考下

以下一些php函数是我们it动力最常用的项目开发函数,这些函数还算是在比较多的项目中使用到的,也是比较通用的。
1.请求接口的处理函数

复制代码 代码如下:


/**
* 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);
}


2.获取文件扩展名

复制代码 代码如下:


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


3.可逆的加密、解密函数

复制代码 代码如下:


/**
* 字符串加密
* @param $string 需加密的字符
* @param $operation 加密或解密
* @param $key 网站加密key,香港空间,美国空间,防止破解
* @return string
*/
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : '^$');
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $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), 0, 16) . $string;
$string_length = strlen($string);

$result = '';
$box = range(0, 255);

$rndkey = array();
for ($i = 0; $i $rndkey[$i] = ord($cryptkey[$i % $key_length]);
}

for ($j = $i = 0; $i $j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for ($a = $j = $i = 0; $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, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26) . $keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc . str_replace('=', '', base64_encode($result));
}
}


4.字符串转十六进制

复制代码 代码如下:


/**
* 字符串转十六进制
* @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 $r .= ($hexes [(ord($s{$i}) >> 4)] . $hexes [(ord($s{$i}) & 0xf)]);
return $r;
}


5.十六进制转字符串

复制代码 代码如下:


/**
* 十六进制转字符串
* @param unknown_type $s
*/
function hex2str($s) {
$r = "";
for ( $i = 0; $i {
$x1 = ord($s{$i});
$x1 = ($x1>=48 && $x1 $x2 = ord($s{$i+1});
$x2 = ($x2>=48 && $x2 $r .= chr((($x1 }
return $r;
}


6.返回经addslashes处理过的字符串或数组

复制代码 代码如下:


/**
* 返回经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);
}


7.返回经stripslashes处理过的字符串或数组

复制代码 代码如下:


/**
* 返回经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);
}


8.返回经 htmlspecialchars 处理过的字符串或数组

复制代码 代码如下:

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn