$text = "<p>keep me</p> <p>strip me </p>
$pattern = "/<[^\/>]*>(\ \;)*<\/[^>]*>/";
$text = preg_replace($pattern, '', $text);
Hi, I need to strip "quasi-empty" 开发者_开发知识库p tags from a html string. there's always only a 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
along with any accompanying text, as per your example.
$text = "<p>keep me</p> <p>strip me </p>";
$pattern = "/<p>[^<]* \;[^<]*<\/p>/";
$output = preg_replace($pattern, '', $text);
If you actually want it to only strip out <p> </p>
blocks with
and spaces, use the following pattern instead:
$pattern = "/<p>(\s* \;\s*)+<\/p>/";
If you want to only strip out <p> </p>
blocks that have an
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."} \;[^<]{0,".$maxCharsAfter."}<\/p>/";
$text = preg_replace("!<p>(?: )*</p>!", "", $text);
$text = "<p>keep me</p> <p>strip me </p>";
str_replace(' ','',$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 </p><div class=\"someclass\">div</div>";
$newtext = "";
preg_match_all("/(\<.*?>.*?<\/.*?>)/",$text,$matches);
foreach($matches[0] as $tag)
{
if(!strstr($tag,' '))
{
$newtext .= $tag;
}
}
echo $newtext;
?>
精彩评论