检测字符串中的HTML标签 [英] Detect HTML tags in a string
本文介绍了检测字符串中的HTML标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要检测字符串是否包含HTML标记.
I need to detect whether a string contains HTML tags.
if(!preg_match('(?<=<)\w+(?=[^<]*?>)', $string)){
return $string;
}
上面的正则表达式给我一个错误:
The above regex gives me an error:
preg_match() [function.preg-match]: Unknown modifier '\'
我对正则表达式不太了解,所以不确定是什么问题.我尝试转义\,但它没有执行任何操作.
I'm not well up on regex so not sure what the problem was. I tried escaping the \ and it didn't do anything.
有没有比正则表达式更好的解决方案?如果没有,那么与preg_match一起使用的正确正则表达式是什么?
Is there a better solution than regex? If not, what would be the correct regex to work with the preg_match?
推荐答案
一个简单的解决方案是:
A simple solution is:
if($string != strip_tags($string)) {
// contains HTML
}
与正则表达式相比,这样做的好处是更易于理解,但是我无法评论两种解决方案的执行速度.
The benefit of this over a regex is it's easier to understand, however I could not comment on the speed of execution of either solution.
这篇关于检测字符串中的HTML标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文