PHP的一些 有用但不常用的函数记录
1. microtime()
当前 Unix 时间戳以及微秒数。
<?php
$mem = new Memcache;
$mem->connect("127.0.0.1", 11211);
$time_start = microtime_float();
//保存数据
for($i = 0; $i < 100000; $i ++){
$mem->set("key$i",$i,0,3);
}
$time_end = microtime_float();
$run_time = $time_end - $time_start;
echo "用时 $run_time 秒\n";
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
?>
2. version_compare 比较版本号专用,可以不用考虑一些字符串的长度是否对应,其中第三个可选参数是比较符
判断 第一个 是否比 第二个 第三个参数
$a = version_compare('1.0.2', '1.0.1','>');
var_dump($a); // boolean true
3. stripslashes 去除字符中的 '\',主要用于对一些传过来的数据进行格式化
$str = stripslashes($_POST['data']);
if(!$str){
echo '{"result":"0"}';
return false;
}
$arr = json_decode($str, true);
//先去排除一些肯定不是游戏的 直接在原数组中移除 遍历后的就是可以使用的数组
foreach($arr as $k=>$v){
$reg = '/\.\w+\./';
preg_match($reg,$v['bundleId'],$matches);
$fenge = $matches[0];
if(str_exists($not,$fenge) && $fenge){
unset($arr[$k]);
}
}
4.字符串截取
$findme = strpos($v['url'], '&id='); $id = substr($v['url'],$findme+4);
5.使用过的hash验证函数
function str2hash($str){
if(!$str){
return 0;
}
$hash = 4685;
for ($i=0; $i<strlen($str); $i++) {
$tmp = substr($str,$i,1);
$hash = ($hash * 25) ^ ord($tmp);
}
return $hash;
}

浙公网安备 33010602011771号