2012-01-11 79 views
0

任何人都可以帮助我找到任何以HTML格式显示在文本中的标签。PHP html标签查找

例:$text="<a href>the hero</a>";

发现 “<a” 存在于$text或没有在PHP。

+0

你想知道你的字符串中是否有标签?删除标签?或知道哪些标签在那里?根据你的答案,真正的答案会有所不同 – 2012-01-11 21:43:37

+0

有点点:http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html – Wrikken 2012-01-11 21:45:37

+0

我想检查“ user1017808 2012-01-11 21:50:40

回答

-2

可能:

if(str_replace("<a", "<a", $text)) { 
    echo "ok"; 
} else { 
    echo "not ok"; 
} 

the manual for str_replace

+0

您正在使用'str_replace'进行不必要的工作 - 用相同的字符串替换字符串。使用这种构造会降低代码的可读性。 – 2012-01-11 21:54:12

0

为了解析HTML,最好使用某种DOM解析器,这里更多的信息:PHP HTML DOM Parser,但如果你的目标只是要检查HTML标签的存在,preg_match可以成为解决方案:

$text="<a href>the hero</a>"; 
var_dump(preg_match("/\<a.*\>(.*?)\<\/a\>/si", $text)); 
1

如果你只是想检查字符串使用strpos中是否存在“< a”。它比preg_ *快得多,它必须首先编译一个正则表达式。

<?php 
$exists = (strpos($text , '<a') !== false); 
?>