首页 > 编程技术 > php

PHP 正则匹配unicode 编码正则方法

发布时间:2016-11-25 17:40

php教程 正则匹配unicode 编码正则方法,因为unicode编码的特殊性,所以一般的中文或英文正则是不能正确取到我们想要的内容的,下面来看一款专业的unicode正则表达式吧。

看个unicode编码图。

$words = "0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrsruvwxyz!@#$%^&*()_+-=[],./{}|<>?'"你好啊我们";
$otherstr=preg_replace("//[x{0080}-x{00ff}]+/iu"," ",$words);
echo 'otherstr:',$otherstr;

php教程自带了json_encode来处理json数据,那么它们对中文的支持不好,下面我们来看个实例

echo json_encode(array(123213,'中国'));


{"platformid":"123213","userid":"1023","username":"u00b7u00f0u00b5u00b2u00c9u00b1u00b7u00f0u00ccu00fc"}

我们会发现,英文可以正确解析,但中文就出来u00b7u00f0u00b5u00b2u00c9u00b1u00b7u00f0u00ccu00fc哦,这可能是unicode编码,但我没测试过只是自己猜的。下面我们来看一款解决json中文乱码的函数

private function to_utf8($in)
{
if (is_array($in)) {
foreach ($in as $key => $value)
{
$out[$this->to_utf8($key)] = $this->to_utf8($value);
}
}
elseif(is_string($in))
{
if(mb_detect_encoding()($in) != "utf-8")
return utf8_encode($in);
else
return $in;
}
else
{
return $in;
}
return $out;
}

1.把$usr->username直接输出,页面头设置charset=utf-8.乱码
2.echo json_encode($usr)输出username=null
3.页面头设置为charset=gbk,输出正确->可以确定原编码为gbk
最后通过ie,chrome,firefox测试得出结论:


1.保证页面字符集与数据库教程一致,输出一定正常 。
2.做json_encode时保证数据编码是utf-8,json_decode正常。
3.如果要对非utf-8字符做json_encode,先转换成utf-8。
4.对非utf-8字符做json_decode的时候,千万不能忘记转换成原先的编码,否则会输出乱码!!

php教程系统时区8小时差设置方法
修改php系统时区的两种方式:
1、修改php.ini文件中的设置,找到[date]下的“;date.timezone=”,将该项修改为date.timezone=asia/hong_kong (prc 中国时间),然后重新启动apache服务器。

2、在应用程序中,使用时间日期函数之前添加“date_default_timezone_set(“asia/hong_kong”)”函数

修改php.ini文件后获取本地时间:
php代码:
<?php
echo “现在是北京时间:”.date(“y-m-d h:i:s”).”<p>”;
?>
显示结果:
现在是北京时间:2010-11-30 20:50:03 (与本地时间相符)
php.ini修改后的代码:
[date]
; defines the default timezone used by the date functions
; http://php.net/date.timezone
date.timezone = prc

下面看看更多方法

1.在php.ini文件中修改设置。
[date]
   //defines the default timezone used by the date functions
   date.timezone = asia/shanghai
这里真是。。。可以设置shanghai,chongqin,hong_kong,taibei...等等,可就是没有找到beijing,不知道是什么原因。

2.在php程序中运行时设置。
<?php
    if(date_default_timezone_get() != "1asia/shanghai") date_default_timezone_set("asia/shanghai");
?>
注意:用"date_default_timezone_get"获得的时间设置中有一个1,也不太清楚是杂回事呢。

3.在使用时间时自己多加8*3600秒也是行滴。
<?php
    echo date("y-m-d h:i:s",time()+8*3600);
?>

注:
自php5.0开始,用php获取系统时间时,时间比当前时间少8个小时。原因是php.ini中没有设置timezone时,php是使用的utc时间,所以在中国时间要少8小时

 

装php教程扩展:windows 下的 memcache步骤

1.下载php的扩展dll:http://downloads.php.net/pierre/

2.将.dll放入php的扩展目录下面,一般在php目录中的ext 文件夹中。

3.修改apache目录下bin文件夹中的php.ini,加上extension=php_memcache.dll即可。

4.重启apache服务器,用phpinfo()查看,出现memcache说明配置正确.

值得注意的是:

 必须注意php的编译版本,下载memcache的时候注意要使用同样的编译版本;比如php是使用vc6编译的,那么memcache也应该使用vc6编译版本

下面来看一篇在windows下载安装memcache php教程吧

windows下的memcache安装:
1. 下载memcache的windows稳定版,解压放某个盘下面,比如在c:memcached
2. 在终端(也即cmd命令界面)下输入 ‘c:memcachedmemcached.exe -d install’ 安装
3. 再输入: ‘c:memcachedmemcached.exe -d start’ 启动。note: 以后memcached将作为windows的一个服务每次开机时自动启动。这样服务器端已经安装完毕了。
4.下载php_memcache.dll,请自己查找对应的php版本的文件
5. 在c:winntphp.ini 加入一行 ‘extension=php_memcache.dll’
6.重新启动apache,然后查看一下phpinfo,如果有memcache,那么就说明安装成功!


memcached的基本设置:

-p 监听的端口
-l 连接的ip地址, 默认是本机
-d start 启动memcached服务
-d restart 重起memcached服务
-d stop|shutdown 关闭正在运行的memcached服务
-d install 安装memcached服务
-d uninstall 卸载memcached服务
-u 以的身份运行 (仅在以root运行的时候有效)
-m 最大内存使用,单位mb。默认64mb
-m 内存耗尽时返回错误,而不是删除项
-c 最大同时连接数,默认是1024
-f 块大小增长因子,默认是1.25
-n 最小分配空间,key+value+flags默认是48
-h 显示帮助

memcache环境测试:
运行下面的php文件,如果有输出this is a test!,就表示环境搭建成功。开始领略memcache的魅力把!

< ?php
$mem = new memcache;
$mem->connect(”127.0.0.1′, 11211);
$mem->set(’key’, ‘this is a test!’, 0, 60);
$val = $mem->get(’key’);
echo $val;
?>

include_path 下寻找,然后是当前运行脚本所在目录相对的 include_path 下寻找。例如 include_path 是 . ,当前工作目录是 /www/ ,脚本中要 include 一个 include/a.php教程  并且在该文件中有一句 include "b.php" ,则寻找 b.php  的顺序先是 /www/ ,然后是 /www/include/ 。如果文件名以 ./  或者 ../  开始,则只在当前工作目录相对的 include_path 下寻找。

所以如下所示的文件结构

----a.php

----include/b.php

----include/c.php

 其中a.php

<?php

include 'include/b.php';

?>

-----------------------

b.php

<?php

include 'c.php';

include 'include/c.php';

?>

--------------------------

c.php

<?php

echo 'c.php';

?>

--------------------------

都能正确运行,说明b.php中两种不同包含路径都是可行的,根据include寻找包含文件的方式都能找到c.php。

但是最好的方式还是使用绝对路径,如果使用了绝对路径,php内核就直接通过路径去载入文件而不用去include path逐个搜索文件,增加了代码执行效率

<?php

define('root_path',dirname(__file__));

include root_path.'/c.php';

?>


结论:

显然include 后面路径的格式和php的include path 对程序性能都是存在影响的,include 性能从慢到快的排序是

include 'a.php' < include './a.php' < include '/fullpath/a.php

在代码中,使用绝对路径include文件是最好的选择,因为这样php内核就直接通过路径去载入文件而不用去include path逐个搜索文件。

所以我们最好在项目的公用文件中定义一个项目根目录绝对路径的常量,然后所有的include的路径前都带上这个常量,这样项目中所有的include使用的都是绝对路径,既提高程序性能,也减少了相对路径带来的烦恼。

参考代码(来自emlog):

define('emlog_root', dirname(__file__));

include emlog_root . '/config.php';

如果你的项目中已经大量使用include 'test.php'  这样格式的相对路径且不好大量修改,那么请尽量减少php include path中的路径以提高一定的include性能。因为include path中的路径越少,php搜索文件的时间也越少。

标签:[!--infotagslink--]

您可能感兴趣的文章: