正则如何匹配中文字在某中文字符串中?
例如:
$str = '正则如何匹配中文字在某中文字符串中?';
如何匹配 '中' 字在 $str 中?
谢谢
--------------------------------------------------------------
1
2$str = '正则如何匹配中文字在某中文字符串中?';
3preg_match("/中/",$str,$regs);
4print_r($regs);
输出
Array
(
[0] => 中
)
---------------------------------------------------------------
对楼主补充问题的解答
1
2$str = '这里是中文 this is English';
3//把中文变成红色
4echo preg_replace("/([\x80-\xff].)/","
<font color="red">\\1</font>
1",$str);
2//把非中文变成红色
3echo preg_replace("/([\x20-\x7f])/","
<font color="red">\\1</font>
1",$str);
对gowuliao(吴辽) 问题的解答
$test=mb_ereg_replace($key,"
1<font color="red">".$key."</font>
",$str);
echo $test;
php对宽字符处理的不太好,但有一个补救的办法——连接mbstring扩展库