PHP正则表达式删除最后一段(具有属性)和内容

PHP正则表达式删除最后一段(具有属性)和内容,php,regex,Php,Regex,我的问题类似于Stackoverflow上的问题。但这是有区别的 我将以下内容存储在MySQL表中: <p align="justify">First paragraph</p> <p>Second paragraph</p> <p>Third paragraph</p> <div class="item"> <p>Some paragraph here</p> <p><

我的问题类似于Stackoverflow上的问题。但这是有区别的

我将以下内容存储在MySQL表中:

<p align="justify">First paragraph</p>
<p>Second paragraph</p>
<p>Third paragraph</p>
<div class="item">
<p>Some paragraph here</p>
<p><strong><u>Specs</u>:</strong><br /><br /><strong>Weight:</strong> 10kg<br /><br /><strong>LxWxH:</strong> 5mx1mx40cm</p
<p align="justify">second last para</p>
<p align="justify">This is the paragraph I am trying to remove with regex.</p>
</div>
我试图删除表中每一行的最后一段标记和内容。链接问题中提到的最佳答案建议遵循正则表达式-

preg_replace('~(.*)<p>.*?</p>~', '$1', $html)
与链接问题的区别在于-有时我的最后一段标记可能有或可能没有属性align=justify。如果最后一段具有此属性,则所述解决方案将删除内容中没有属性的最后一段。因此,无论最后一段的属性状态如何,我都在努力寻找删除该段的方法。

将正则表达式更改为:

preg_replace('~(.*)<p[^>]*>.*</p>\R?~s', '$1', $html)
正则表达式突破


@lucastrezesniewski的可能副本感谢链接。虽然我没有完全理解它,但我已经把它添加到了书签中。链接基本上说你应该使用合适的工具来完成这项工作。这里需要一个HTML解析器/DOM操作库。使用正则表达式很脆弱——使用DOM、XPath或CSS选择器可以做得更好、更容易。@Lucastzesniewski感谢您的简化。我将阅读有关HTML解析器/DOM操作的内容。谢谢。成功了。我想你需要编辑答案并从regex=>**strong text**@Dr.AtulTiwari中删除此文本:谢谢,奇怪的是,当我粘贴一些东西时会发生这种情况!
~           # Opening regex delimiter
  (.*)      # Select any chars matching till the last '<p>' tags
            # (actually it matches till the end then backtrack)
  <p[^>]*>  # select a '<p>' tag with any content inside '<p .... >'
            # the content chars after '<p' must not be the literal '>'
  .*        # select any char till the '</p>' closing tag
  </p>      # matches literal '</p>'
  \R?       # select (to remove it) any newline (\r\n, \r, \n)
~s          # Closing regex delimiter with 's' DOTALL flag 
            # (with 's' the '.' matches also newlines)