\n";
echo "
| 项目 | 比值 | 总用时(秒) | 平均用时(微秒) | 返回值 |
\n";
}
$base = 0;
foreach($testers as $tester){
list($title, $func) = $tester;
$start = microtime(true);
for($i = 0; $i < $times; $i++){
$rtn = $func($i);
}
$total = microtime(true) - $start;
if(!$base) $base = $total;
$percent = $total / $base * 100;
$avg = $total / $times * 1000 * 1000;
ob_start();
if(self::inCLI()){
$s = str_repeat(' ', $titleMaxLen - self::strSpace($title));
printf("%s%s%.2f%s\t\t%.4f sec\t%.2f us\t\t%s\n", $title, $s, $percent, '%', $total, $avg, $rtn);
}else{
printf("| %s | %.2f%s | %.4f sec | %.4f us | %s |
\n", $title, $percent, '%', $total, $avg, $rtn);
}
ob_end_flush();
}
if(!self::inCLI()) echo "";
}
/**
* 检查当前脚本是否在命令行环境中执行
* @return bool
*/
public static function inCLI(){
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])){
return true;
} else {
return false;
}
}
/**
* 获取客户端IP
* @return string
*/
public static function clientIp(){
return getenv('HTTP_CLIENT_IP')?:
getenv('HTTP_X_FORWARDED_FOR')?:
getenv('HTTP_X_FORWARDED')?:
getenv('HTTP_FORWARDED_FOR')?:
getenv('HTTP_FORWARDED')?:
getenv('REMOTE_ADDR');
}
/**
* 产生一个随机字串
* @param int $len 指定随机字串的长度
* @param string $scope 随机字符的取值范围
* @return string
*/
public static function randString($len, $scope = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890") {
$strLen = strlen($scope) - 1;
$string = '';
for($i = 0; $i < $len; $i ++){
$string .= substr($scope, mt_rand(0, $strLen), 1);
}
return $string;
}
/**
* 字串长度计算(可计算utf8)
* 一个汉字(或是其它非ascii)的长度计为1
* 注意:此函数性能不高,不要用于大量调用的情况
* @param string $str 待计算的字串
* @return int 长度
*/
public static function utf8strlen($str) {
$i = 0;
$count = 0;
$len = strlen($str);
while($i < $len){
$chr = ord($str[$i]);
$count++;
$i++;
if($i >= $len) break;
if($chr & 0x80){
$chr <<= 1;
while($chr & 0x80){
$i++;
$chr <<= 1;
}
}
}
return $count;
}
/**
* 返回一个中英文混合字符串的占位长度
* 一个汉字(或是其它非ascii)占两个ascii字符的位置
* 注意:此函数性能不高,不要用于大量调用的情况
* @param string $str 待计算的字串
* @return int 占位长度
*/
public static function strSpace($str){
return (strlen($str) + self::utf8strlen($str)) / 2;
}
/*
* 截取中文字符
* @param string $str 字串
* @param int $len 截取长度
* @return string 截取后的字串
*/
public static function cnSubstr($str, $len){
for($i = 0; $i < $len; $i ++){
$temp_str = substr($str, 0, 1);
if (ord($temp_str) > 127) {
$i ++;
if($i < $len){
$new_str[] = substr($str, 0, 3);
$str = substr($str, 3);
}
}else{
$new_str [] = substr($str, 0, 1);
$str = substr($str, 1);
}
}
return join($new_str);
}
/**
* 将Y-m-d H:i:s格式的时间转成unixtime
* @param string $datatime 日期时间,格式: Y-m-d 或 Y-m-d H:i:s
* @return int
*/
public static function unixtime($datetime){
$d = explode(' ', $datetime);
$date = explode('-', $d[0]);
if(isset($d[1])){
list($h, $i, $s) = explode(':', $d[1]);
return mktime($h, $i, $s, $date [1], $date [2], $date [0]);
}
return mktime(0, 0, 0, $date [1], $date [2], 0 + $date [0]);
}
/**
* 判断某个字串是否Y-m-d格式的时间字串
* @param string $date 日期
* @return bool
*/
public static function isDate($date){
$dPat = '([1-9])|((0[1-9])|([1-2][0-9])|(3[0-2]))';
$mPat = '([1-9])|((0[1-9])|(1[0-2]))';
$yPat = '(19|20)[0-9]{2}';
$pattern = "!^($yPat)-($mPat)-($dPat)$!";
return preg_match($pattern, $date);
}
/**
* 判断某个字串是否Y-m-d H:i:s格式的时间字串
* @param string $datetime 日期时间
* @return bool
*/
public static function isDatetime($datetime){
$dPat = '([1-9])|((0[1-9])|([1-2][0-9])|(3[0-2]))';
$mPat = '([1-9])|((0[1-9])|(1[0-2]))';
$yPat = '(19|20)[0-9]{2}';
$hPat = '([1-9])|(([0-1][0-9])|(2[0-3]))';
$mPat = '([1-9])|([0-5][0-9])';
return preg_match("!^($yPat)-($mPat)-($dPat) ($hPat):($mPat):($mPat)$!", $datetime);
}
public static function captcha($code, $height = 35){
$colorMin = 0;
$colorMax = 230;
$fonts = ['fleck.ttf', 'molten.ttf', 'oneway.ttf', 'sixty.ttf'];
$len = strlen($code);
$width = $height * $len;
$size = round($height * 0.70);
$offset = round($size / $len);
$image = imagecreate($width, $height);
imagecolorallocate($image, 233, 233, 233);
// 生成随机线条
// for($i=0; $i