PHP正则表达式贪婪的问题

时间:2013-10-12 14:10:39

标签: php regex

我想删除所有看起来像这样的html:

<p><font face="Arial" size="2"><a href="#top">Back to the top</a></font></p>

字体和大小各不相同,也可能还有其他一些东西。

为了确保,我正在使用以下正则表达式:

/<p.*?><font.*?><a href=.*?>Back.*?<\/a><\/font><\/p>/is

但是,它匹配我正在搜索的完整文本字符串,例如:

<p>However, if this is our reaction, we will indeed be weak in our weaknesses. For the strength of Christ does not come to rest upon us in the midst of discontent and rebellion.</p> <p>Seeing the great design of the Lord in affliction, let us rather take pleasure in our afflictions. Let us glory, that is boast, in them, and in wisdom let us then turn to Jesus Christ our Savior to receive His great strength.</p> <p>Then in our weakness we will be indeed very strong!</p> <p><font face="Arial" size="2"><a href="#top">Back to the top</a></font></p> <hr> 

我做错了什么?!?

1 个答案:

答案 0 :(得分:1)

将点更改为[^&gt;]

/<p[^>]*?><font[^>]*?><a href=[^>]*?>Back[^>]*?<\/a><\/font><\/p>/is