how can i strip same tags as parent tag using preg_replace ? for example i have a tag called <strip>
and i want to strip any child tags that are <strip>
for example <strip><strip>Avoid my tag</strip></strip>
i want that to become like that --> <strip>Avoid my tag</strip>
i don't know that much about preg_* but thats what i already have:
preg_replace_callback(
'#\<strip\>(.+?)\<\/strip\>#s',
create_function(
'$matches',
'return "<strip>".htmlentities($matches[1])."</strip>";'
),
$content
);
this little function will apply htmlentities with everything inside <strip>
tags and idon't want <strip>
tag to be repeated inside each other
thanks
Please dont user regex for Html dom, take a look at DOMXPath
the doc is HERE
a shot exemple here :
$doc = new DOMDocument();
$doc->loadHTMLFile($file);
$xpath = new DOMXpath($doc);
$elements = $xpath->query("/html/body/*");
foreach ($elements as $element) {
$nodes = $element->childNodes;
foreach ($nodes as $node) {
//Do your stuff here :)
echo $node->nodeValue. "
";
}
}
If it's some XML take a look at SimpleXMLElement
HERE
If you want regex, do so
$str = 'abc<strip><strip><strip>Avoid my tag</strip></strip></strip>def';
echo preg_replace('
/((<strip>(?=<strip>))*)(<strip>[^<]+<\/strip>)(((?<=<\/strip>)<\/strip>)*)/',
'\3', $str); // abc<strip>Avoid my tag</strip>def
UPDATE One more step
echo preg_replace_callback(
'/((<strip>(?=<strip>))*)<strip>([^<]+)<\/strip>(((?<=<\/strip>)<\/strip>)*)/',
create_function(
'$matches',
'return "<strip>".htmlentities($matches[3])."</strip>";'
),
$content
);