Regularly remove style tag images from Html

I am new to Regex, but decided that this is the easiest way to what I need to do. Basically I have a line (in PHP) that contains all the HTML code ... I want to remove any tags that have the style = display: none ...

eg,

<img src="" style="display:none" />

<img src="" style="width:11px;display: none" >

etc...

So far, my regex is:

<img.*style=.*display.*:.*none;.* >

But this seems to leave the html bit behind, and also removes the next element when used in php with preg_replace.

+3
source share
4 answers

, Regex . Regex , . <foo> , >foo<, . .

DOM :

$html = <<< HTML
<img src="" style="display:none" />
<IMG src="" style="width:11px;display: none" >
<img src="" style="width:11px" >
HTML;

() . DOM :

$dom = new DOMDocument();
$dom->loadHtml($html);
$dom->normalizeDocument();

DOM "IMG", "style", "display". "display: none" XPath, :

$xpath = new DOMXPath($dom);
foreach($xpath->query('//img[contains(@style, "display")]') as $node) {
    $style = str_replace(' ', '', $node->getAttribute('style'));
    if(strpos($style, 'display:none') !== FALSE) {
        $node->parentNode->removeChild($node);
    }
}

IMG . , "display: none", , DOM.

HTML:

echo $dom->saveHTML();

:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><img src="" style="width:11px"></body></html>

Regex!


: XML- CSS

+4
$html = preg_replace("/<img[^>]+style[^>]+none[^>]+>/", '', $html);
+3

<img> , ; regexp , HTML.

, , , , , > . *, , , > , > .

. * [^ > ] *, . (, , , ).

0

; .* " -", :

<img src="foo.png" style="something">Some random displayed text : foo none; bar<br>

, , , , [^>]* .*. , , HTML, DOMDocument

0

Source: https://habr.com/ru/post/1744137/


All Articles