Home > Backend Development > PHP Tutorial > 正则表达式 - 一个PHP正则的问题

正则表达式 - 一个PHP正则的问题

WBOY
Release: 2016-06-06 20:33:25
Original
1069 people have browsed it

//排除图片中的关键词

<code>$content = "<p><img alt="a你好bb" class="fr-fin fr-dib" src="http://frbird.qiniudn.com/topic/150609/5576afcd7fd32e334ece5f67-hd.jpg"><";

$content = preg_replace("/(<img\s[^>]*?)(你好)([^>]*?>)/", '$1%&&&&&%$3', $content);
</code>
Copy after login
Copy after login

大家帮忙看下这段代码有问题吗,我是想img标签里的关键词,如'你好'替换掉,却总是无法正确执行! html标签已都转为实体,如已解析成<>

回复内容:

//排除图片中的关键词

<code>$content = "<p><img alt="a你好bb" class="fr-fin fr-dib" src="http://frbird.qiniudn.com/topic/150609/5576afcd7fd32e334ece5f67-hd.jpg"><";

$content = preg_replace("/(<img\s[^>]*?)(你好)([^>]*?>)/", '$1%&&&&&%$3', $content);
</code>
Copy after login
Copy after login

大家帮忙看下这段代码有问题吗,我是想img标签里的关键词,如'你好'替换掉,却总是无法正确执行! html标签已都转为实体,如已解析成<>

先转换回去再匹配就好了嘛。。。

<code>php</code><code>echo preg_replace('/[\x{4e00}-\x{9fa5}]+/u','%&&&&&%', $content);
</code>
Copy after login

$str = "!工aaa工地";
preg_match_all("/([\x{4e00}-\x{9fa5}])/u",$str,$match);//匹配UTF8中文汉字
var_dump($match);
exit;
做中文匹配的时候,会有多方面的影响比如编码格式(utf-8,unicode,gbk,big5等)
如果不确定要找的范围, 就上网找对应的编码的编码表

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template