正则表达式 - php 正则怎么匹配非html标签的"<",">"符号

WBOY
Release: 2016-06-06 20:43:51
Original
1289 people have browsed it

需要把一段文本里的尖括号转义一下,但是部分HTML标签需要留下,例如<br> <p></p>等,怎么写一个可扩展的正则?例如目前只需要排除<br> <p></p>, 如果还需要多排除一个HTML标签直接在pattern上多加一组就可以?(文本比较混乱不适于用HTML解析)
网上查到一个 ?[^pP/>]+> 好像不对
或者有相应的函数或方法推荐吗?

回复内容:

需要把一段文本里的尖括号转义一下,但是部分HTML标签需要留下,例如<br> <p></p>等,怎么写一个可扩展的正则?例如目前只需要排除<br> <p></p>, 如果还需要多排除一个HTML标签直接在pattern上多加一组就可以?(文本比较混乱不适于用HTML解析)
网上查到一个 ?[^pP/>]+> 好像不对
或者有相应的函数或方法推荐吗?

preg_replace("//i","& l t;$1& g t;",$str);

可以使用php自带的strip_tags函数
http://php.net/manual/zh/function.strip-tags.php


可能是这个意思,纯正则比较麻烦,可以写程序处理

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!