tags:

views:

156

answers:

3
$text = "<p>keep me</p> <p>strip me &nbsp;</p>
$pattern = "/<[^\/>]*>(\&nbsp\;)*<\/[^>]*>/"; 
$text =  preg_replace($pattern, '', $text);

Hi, I need to strip "quasi-empty" p tags from a html string. there's always only a &nbsp; as a trigger in the p element. how can I strip it with regex?

A: 
$text = "<p>keep me</p> <p>strip me &nbsp;</p>";
str_replace('&nbsp;','',$text);

job done

Yo have a lot of learning to do: http://www.regular-expressions.info/conditional.html


<?php
$text = "<p>keep me</p> <p>strip me &nbsp;</p><div class=\"someclass\">div</div>";
$newtext = "";
preg_match_all("/(\<.*?>.*?<\/.*?>)/",$text,$matches);
foreach($matches[0] as $tag)
{
    if(!strstr($tag,'&nbsp;'))
    {
        $newtext .= $tag;
    }
}
echo $newtext;
?>
RobertPitt
I think he want's to remove the p-tags as well... but yeah.
roe
I suspect that fails on nested tags.
MikeD
yep, but i just gave him a snip for the string he posted, he needs a DOM traverse tool to do it
RobertPitt
+1  A: 
$text  = preg_replace("!<p>(?:&nbsp;)*</p>!", "", $text);
M42
will not work..
RobertPitt
In wich way ? According to OP, he wants to remove p tags that only contain  
M42
i tested it, and it dont work, thats all :)
RobertPitt
+1  A: 

The following pattern will match all <p> </p> blocks that include &nbsp; along with any accompanying text, as per your example.

$text = "<p>keep me</p> <p>strip me &nbsp;</p>";
$pattern = "/<p>[^<]*&nbsp\;[^<]*<\/p>/"; 
$output =  preg_replace($pattern, '', $text);

If you actually want it to only strip out <p> </p> blocks with &nbsp; and spaces, use the following pattern instead:

$pattern = "/<p>(\s*&nbsp\;\s*)+<\/p>/"; 

If you want to only strip out <p> </p> blocks that have an &nbsp; and up to a certain number of characters, use the following (setting the $maxChars variables as you see fit):

$maxCharsBefore = 10;
$maxCharsAfter = 10;
$pattern = "/<p>[^<]{0,".$maxCharsBefore."}&nbsp\;[^<]{0,".$maxCharsAfter."}<\/p>/";
JGB146
cool, that one behaves the way I want. thanks to all the posters!
Steve