Reputation: 15166
Maybe a newbie question:
I have a string like:
$string = '<p>this is what we need.</p><p> </p>';
How can I remove the last characters, i.e. <p> </p>
, using PHP regex (not substr)?
I found a similar question here: remove <br>'s from the end of a string
with solution: preg_replace('/(<br>)+$/', '', $string);
But changing this to: preg_replace('/(<p> </p>)+$/', '', $string);
does not work.
It throws PHP Warning: preg_replace(): Unknown modifier 'p'
I guess I am missing some escaping? Of the <> or the slashes?
Thanks for your help.
Upvotes: 4
Views: 10265
Reputation: 5397
You are using the slash character as a regex delimiter and also as part of your regex (in the closing p tag) so you should escape it. So:
/(<p> </p>)+$/
should be
/(<p> <\/p>)+$/
And also it seems that this is not the kind of job for a regex, but it's your call.. str_replace or str_ireplace would do the job just fine
Upvotes: 7
Reputation: 89557
A good way to deal with slashes when you proceed on html content (or url) is to use other delimiters than slashes, example:
$result = preg_replace('~(?><p> </p>)+$~', '', $string);
then you don't need to escape the slashes.
Note that you can delete all that is useless with:
$result = preg_replace('~
(?>
<(\w++)[^>]*+>(?>\s++| |<br\s*+/?>)*</\1> # empty tags
| # OR
(?>\s++| |<br\s*+/?>)+ # white spaces, br,
)+$
~xi', '', $string);
Upvotes: 0
Reputation: 22972
Not using regexes, but guaranteed to remove only the string when it is at the end:
$string = '<p> </p><p>the previous and next   should remain</p><p>nbsp</p><p>this is what we need.</p><p> </p><p> </p><p> </p><p> </p>';
$lenString = strlen($string);
$remove = '<p> </p>';
$lenRemove = strlen($remove);
while (strrpos($string, $remove) === $lenString - $lenRemove) {
$string = substr($string, 0, -$lenRemove);
$lenString = strlen($string);
}
echo $string, PHP_EOL;
this prints <p> </p><p>the previous and next   should remain</p><p>nbsp</p><p>this is what we need.</p>
Upvotes: 0
Reputation: 1260
Simple way you can do
$string = '<p>this is what we need.</p><p> </p>';
$string = str_replace('<p> </p>','',$string);
Upvotes: 3
Reputation: 9031
This should do the trick:
$string = '<p>this is what we need.</p><p> </p>';
$pattern = "/<p[^>]*>[\s| ]*<\/p>/";
$string = preg_replace($pattern, '', $string);
This would replace all empty <p>
, not just those containing
Upvotes: 0