Php 用于查找包含字符串的元素的正则表达式

Php 用于查找包含字符串的元素的正则表达式,php,regex,preg-match-all,Php,Regex,Preg Match All,如何查找包含字符串的元素?由正则表达式生成。 例如,我想查找包含字符串的元素: <div class="a"><span id="d">string</span></div> <span style="color: red;">string</span> <p class="c">string</p> 字符串 一串 字符串 它应该返回:string,string,string 谢谢。最接近的: &

如何查找包含字符串的元素?由正则表达式生成。 例如,我想查找包含
字符串的元素:

<div class="a"><span id="d">string</span></div>
<span style="color: red;">string</span>
<p class="c">string</p>
字符串
一串
字符串

它应该返回:
string
string

string

谢谢。

最接近的:

<([^ >]+)[^>]*>string</\1>
]+)[^>]*>字符串
但它可能会在几个格式错误的示例上失败。你应该读一读


HTH

为什么使用正则表达式?PHP没有HTML解析器实现吗?为什么排除,是唯一的例外还是有更多例外?@D.D.C:这是一个例子,my
div
有一些其他元素。我想找到(并删除)哪个包含
字符串