php函数preg_replace() 正则去除汉字

项目中需要用到去除汉字的方法,整理的资料


$file = fopen("hb/hacktea8.txt","r+") or exit("Unable to open file!");
while(!feof($file)){
$line=fgets($file);
$pattern = "/[\x{4E00}-\x{9FFF}]+/u";
echo preg_replace($pattern, '', $line);
echo "<br />";
}
fclose($file);

 
从txt文件中取出每一行,并去除每一行中的非汉字。
1.取出一行用fgets()函数
2.去除非汉字使用正则函数preg_replace()
3.非汉字的正则  /[^\x{4E00}-\x{9FFF}]+/  ,注意php中,不能用\u****表示unicode字符,要用\x{****}
4.u表示修正符
posted @ 2013-06-28 16:10  编程狂热者  阅读(2081)  评论(0编辑  收藏  举报