I need to find and replace all div tags which contains tag img, I tried some regular expression without succes :( this is an example of a regular expression that I tried: /(<div class="indicator"[^<]*>.*<img[^>]*>[^<]*</div>)/g
Help please
Thank you
If you have the potential of having nested div div image like dystroy said then it can't be done properly with a regular expression as it's not actually a regular language. Probably you should use DOM. This is relevant: How do you parse and process HTML/XML in PHP?
Better not to try parsing HTML using RegEx, it can be error prone. Using DOM you can do:
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html); // loads your html
$nodeList = $doc->getElementsByTagName('div');
for($i=0; $i < $nodeList->length; $i++) {
$node = $nodeList->item($i);
$children = $node->childNodes;
foreach ($children as $child) {
if ($child->nodeName == 'img') {
echo "DIV tag contains IMG tag\n";
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.