I have content that is being outputted via WordPress, and it wraps paragraph <p> tags around the content. I need to remove the content within the first <p></p> .. How can I do this?
For example, output is:
<p>Posted by Bob</p>
<p>This is the content here</p>
<p>This is the second paragraph</p>
I’d like to remove the line <p>Posted by Bob</p> and leave the rest.
Generally, you should not parse HTML with regex.
In this case, a regex solution might work fine though:
The important things are
?which makes the.*ungreedy (so that it stop as soon as possible and does not continue until the very last</p>sso that.is able to match new-lines1so that only the first match is removedIf you only want to remove the contents of the tag (so if you want to keep an empty paragraph), simply use
<p></p>as the replacement string.However, as I said this is still not a perfect solution. Think about
<p>tags in comments, or<p>tags that have attributes or even invalid HTML. PHP comes with a DOM parser. If you can use 3rd-party libraries there is also this one, which is quite convenient to use and makes the problem almost trivial:Equally if you just want to empty the tag, but keep it, replace
outertextwithinnertext.