My question is similar to this question asked on Stackoverflow. But there is a difference.
I have the following stored in a MySQL table:
<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>
I'm trying to remove the last paragraph tags and content on every row in the table. The best answer mentioned in the linked question suggests following regex -
preg_replace('~(.*)<p>.*?</p>~', '$1', $html)
The difference from linked question is - Sometimes my last paragraph tag may (or may not) have attributes align="justify"
. If the last last paragraph has this attribute, then mentioned solution removes the last paragraph of the content which does not have attributes. So, I am struggling to find a way to remove last paragraph, irrespective of its attributes status.