分类 php 下的文章

7个鲜为人知却非常实用的PHP函数

概述

PHP有着众多的内置函数,其中大多数函数都被开发者广发使用。但也有一些同样有用却被遗忘在角落,本文将介绍7个鲜为人知功能却非常酷的函数。

1.highlight_string()

当需要在网页中展示PHP代码时,highlight_string()函数就显得非常有用。该函数通过PHP内置定义的颜色,返回函数中代码的高亮显示版本。

<?php  

    highlight_string('<?php echo "hello world" ; ?>');  

    echo highlight_string('<?php echo "hello world" ; ?>',true);  

?>  

2.str_word_count()

这个函数可以方便的将输入的字符串参数中的单词个数返回。

<?php  

    $str = "hello world";  

    echo str_word_count($str);  //输出 2  

?>  

3.levenshtein()

该函数可以方便的返回两个参数之间的levenshtein(编辑距离)。曾经遇到过一个需求,用户在编辑身份证的时候,限制用户只能修改4位数字,使用的就是这个函数。

<?php  

    $idcard='230406198506206797';  

    $newIdcard='230406198506207798';  

    echo levenshtein($idcard,$newIdcard);  //输出 2  

?>  

4.get_defined_vars()

这个函数在调试程序的时候非常有用,它会返回包含所有已定义变量的数组,其中包含环境、系统以及用户自定义变量。

<?php  

    var_dump(get_defined_vars());  

?>  

5.escapeshellcmd()

该函数用来跳过字符串中的特殊符号,防止恶意用户耍花招破解服务器系统。可以搭配exec()与system()函数使用。

<?php  

$command = './configure '.$_POST['configure_options'];  

$escaped_command = escapeshellcmd($command);  

system($escaped_command);  

?>  

6.checkdate()

该函数可以用来检测日期参数的有效性。它可以验证输入的每一个参数的合法性。

<?php  

var_dump(checkdate(12, 31, 2000));  

var_dump(checkdate(2, 29, 2001));  

//输出  

//bool(true)  

//bool(false)  

?>  

7.php_strip_whitespace()

该函数会返回删除了注释与空格后的PHP源码。这对实际代码数量和注释数量的对比很有用。

<?php  

// 注释1  

/* 

 * 注释2 

 */  

echo php_strip_whitespace(__FILE__);  

do_nothing();  

?>  

再加上一个,输出当前已经包含过的文件

get_included_files() 

打印变量的前段时间找半天。。。。
转载自:http://www.lai18.com/content/434290.html

php.ini配置错误查看

...It is not safe to rely on the system’s timezone settings...

没有设置时区使用时间相关的函数就会出现上面的错误,于是去修改php.ini里面的默认时区,但是发现修改无效,时区还是未设置的状态,重启服务的时候也不报错
于是使用了下面的命令

[root@www etc]# php -i|grep zone
PHP:  syntax error, unexpected TC_STRING in /usr/local/Zend/etc/php.ini on line 1
"Olson" Timezone Database Version => 0.system
Timezone Database => internal
Default timezone => Asia/Chongqing
date.timezone => no value => no value

发现文件里面有一个致命错误
去看这个文件的第一行

display_errors = On           [Security]

改成数字就好了

display_errors = 1           [Security]

所以以后可以用php -i来看php配置文件的错误,之前一直觉得 nginx -t 好用来着

php取得json前面有乱码

需要在两个服务器之间通信,但返回结果为json

{"time":1442049120,"content":"\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8","validTime":10}

直接访问服务器端服务器看是ok的
但是在客户端服务器var_dump返回值发现成了

string '锘縶"time":1442049350,"content":"\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8\u54c8","validTime":10}' (length=92)

后来发现是返回的字符串前面有一个BOM导致的
去除BOM函数如下(参考http://www.111cn.net/phper/php-cy/55606.htm)

function removeBOM($str = '')
{
   if (substr($str, 0,3) == pack("CCC",0xef,0xbb,0xbf)) {
       $str = substr($str, 3);
   }
   return $str;
}

php异步http请求

//因为是用fsockopen实现的所以需要自己写http请求
function getPostHeader($url,$data)
{
    //    $URL='http://test.com/001/test.php';
    $URL = $url;
    $data_string = http_build_query($data);
    $referrer="";
    // parsing the given URL
    $URL_Info=parse_url($URL);
    // Building referrer
    if($referrer == "") // if not given use this script as referrer
    $referrer = 'http://test.com';
    // Find out which port is needed - if not given use standard (=80)
    if(!isset($URL_Info["port"]))
    $URL_Info["port"]=80;
    // building POST-request:
    $request = '';
    $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
    $request.="Host: ".$URL_Info["host"]."\n";
    $request.="Referer: $referrer\n";
    $request.="Content-type: application/x-www-form-urlencoded\n";
    $request.="Content-length: ".strlen($data_string)."\n";
    $request.="Connection: close\n";
    $request.="\n";
    $request.=$data_string."\n";
    return $request;
}
$host = "http://localhost";
$url = "http://localhost/b.php";
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    fwrite($fp, getPostHeader($url,array('str'=>123)));
    /*忽略执行结果*/
    //        while (!feof($fp)) {
    //            echo fgets($fp, 128);
    //        }
    sleep(1);//要等nginx的upstream处理完并且把请求交给fastcgi之后,再关闭连接,不然nginx会出现499错误
    fclose($fp);//关闭连接
}

在做这个的时候,出现了几个问题,一个就是nginx出现499的错误,这个已经解决了,参考 http://jianfu.li/archives/use-sock-to-implement-the-async-in-php/,这篇文章还说

补充,其实用fastcgi_finish_request函数就可以实现上面的功能。
有时间再看看
a.php 异步请求多个 b.php ,然后 多个b.php进行运算(多个页面同时运算提高速度),但是运行一段时间后,b.php就会停掉,当时因为放弃这个需求了,没有再找问题所在,什么时候的把这个问题找出来