开发者

strip HTML tags with certain content from string

开发者 https://www.devze.com 2023-01-07 06:15 出处:网络
$text = \"<p>keep me</p> <p>strip me &nbsp;</p> $pattern = \"/<[^\\/>]*>(\\&nbsp\\;)*<\\/[^>]*>/\";
$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?


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>/";


$text  = preg_replace("!<p>(?:&nbsp;)*</p>!", "", $text);


$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;
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消