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.
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
Simple way you can do
$string = '<p>this is what we need.</p><p> </p>';
$string = str_replace('<p> </p>','',$string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With