Output (var $DESC)
<p>erster Absatz</p>
<p>zweiter Absatz</p>
Regex (PHP)
preg_replace("<([a-z][a-z0-9]*)\b[^>]*>(.*?)</\1>{2}", '', $DESC)
I would like to delete only the second p but this regex finds both. Thanks for any help.
Output (var $DESC)
<p>erster Absatz</p>
<p>zweiter Absatz</p>
Regex (PHP)
preg_replace("<([a-z][a-z0-9]*)\b[^>]*>(.*?)</\1>{2}", '', $DESC)
I would like to delete only the second p but this regex finds both. Thanks for any help.
Normally I would just tell you to use an HTML parser instead of regex, but since your requirement is so specific, this can actually be accomplished with regex quite safely.
(?<=<\/p>)\s+<p>[\w ]+<\/p>
https://regex101.com/r/Yqaajy/6
Explanation:
(?<=<\/p>)
- Make sure the rest of the pattern is preceded by a <\p>
ending tag (positive lookbehind).
\s+
- Any number of whitespace characters. Note that this will not match correctly if you have single line mode enabled.
<p>[\w ]+<\/p>
- A paragraph block containing one or more word characters (digits, letters, and underscore) and spaces.
Try this:
$DESC ='<p>erster Absatz</p>
<p>zweiter Absatz</p>';
$DESC = preg_replace('#\</p\>[^\<]*\<p[^\>]*\>(.*?)\</p\>#i', '</p>', $DESC);
echo $DESC; // <p>erster Absatz</p>
` block followed immediately by another `
` block, or does it need to check for any element that occurs twice in a row?
– CAustin Sep 27 '17 at 18:47(.*?)<\/p>` and grab the second result from your `preg_match_all`. But again, I would **highly discourage using regex for this**
– ctwheels Sep 27 '17 at 18:54