Avatar
Avatar

Reputation: 15166

Remove <p> </p> from end of string?

Maybe a newbie question:

I have a string like:

$string = '<p>this is what we need.</p><p>&nbsp</p>';

How can I remove the last characters, i.e. <p>&nbsp</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>&nbsp;</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

Answers (5)

mishu
mishu

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>&nbsp;</p>)+$/

should be

/(<p>&nbsp;<\/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

Casimir et Hippolyte
Casimir et Hippolyte

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>&nbsp;</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++|&nbsp;|<br\s*+/?>)*</\1>  # empty tags
   |                                                # OR
    (?>\s++|&nbsp;|<br\s*+/?>)+                     # white spaces, br, &nbsp;
 )+$
                        ~xi', '', $string);

Upvotes: 0

Carlos Campderr&#243;s
Carlos Campderr&#243;s

Reputation: 22972

Not using regexes, but guaranteed to remove only the string when it is at the end:

$string = '<p>&nbsp</p><p>the previous and next &nbsp should remain</p><p>nbsp</p><p>this is what we need.</p><p>&nbsp</p><p>&nbsp</p><p>&nbsp</p><p>&nbsp</p>';
$lenString = strlen($string);
$remove = '<p>&nbsp</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>&nbsp</p><p>the previous and next &nbsp should remain</p><p>nbsp</p><p>this is what we need.</p>

Upvotes: 0

Rakesh Singh
Rakesh Singh

Reputation: 1260

Simple way you can do

$string = '<p>this is what we need.</p><p>&nbsp</p>';

$string = str_replace('<p>&nbsp</p>','',$string);

Upvotes: 3

Alex
Alex

Reputation: 9031

This should do the trick:

$string = '<p>this is what we need.</p><p>&nbsp</p>';
$pattern = "/<p[^>]*>[\s|&nbsp;]*<\/p>/"; 
$string = preg_replace($pattern, '', $string);

This would replace all empty <p>, not just those containing &nbsp;

Upvotes: 0

Related Questions