Generally, you should not parse HTML with regex.
In this case, a regex solution might work fine though:
$str = preg_replace('~<p>.*?</p>~s', '', $str, 1);
The important things are
- the
? which makes the .* ungreedy (so that it stop as soon as possible and does not continue until the very last </p>
- the modifier
s so that . is able to match new-lines
- the limit
1 so that only the first match is removed
If 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:
require "simple_html_dom.php";
$html = new simple_html_dom($input);
$html->load($input);
$html->find('p', 0)->outertext = "";
$output = $html->save();
Equally if you just want to empty the tag, but keep it, replace outertext with innertext.