php7中使用preg_replace_callback()替代preg_replace()

php7中,preg_replace()不再支持"\e" (PREG_REPLACE_EVAL),需要使用preg_replace_callback()来代替。
 
$content = preg_replace("/{#(.+?)}/eis", '$lang[\'\\1\']',$content) ;
 
这是原来的代码,在php7里不能被正确执行。
 
 
 
$content = preg_replace_callback("/{#(.+?)}/is",function($r)use($lang){ return $lang[$r[1]]; }, $content);
 

 

解决rtrim汉字产生乱码的问题

1、问题重现

echo rtrim('邯郸市','市');

输出: 邯�

2、原因

rtrim函数把参数都转成了UTF8后再进行比较。

对于中文,一般都是先转成Unicode,再根据下表转成UTF8。
UCS-4(UNICODE)编码    UTF-8字节流
U-00000000 – U-0000007F    0xxxxxxx
U-00000080 – U-000007FF    110xxxxx 10xxxxxx
U-00000800 – U-0000FFFF    1110xxxx 10xxxxxx 10xxxxxx
U-00010000 – U-001FFFFF    11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
U-00200000 – U-03FFFFFF    111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
U-04000000 – U-7FFFFFFF    1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx

3、解决方法

var_dump(mb_substr('邯郸市',0,-1,'utf-8'));

PHP 判断客户端是IOS还是Android

<?php

//Detect special conditions devices
$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");
$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");

//do something with this information
if( $iPod || $iPhone ){
    //browser reported as an iPhone/iPod touch -- do something here
}else if($iPad){
    //browser reported as an iPad -- do something here
}else if($Android){
    //browser reported as an Android device -- do something here
}else if($webOS){
    //browser reported as a webOS device -- do something here
}

?>

参考资料:
https://stackoverflow.com/questions/6322112/check-if-php-page-is-accessed-from-an-ios-device