netdjw
netdjw

Reputation: 6007

Perl RegExp remove img tag if content style parameter

I have a Perl RegExp question. Given this HTML code:

<a href="#"><img src="..." alt="..." title="..."></a>
<a href="#"><img src="..." alt="..." style="display: none;" title="..."></a>
<a href="#"><img src="..." alt="..." title="..." style="display: none;"></a>
<a href="#"><img src="..." style="display: none;" alt="..." title="..."></a>
<a href="#"><img style="display: none;" src="..." alt="..." title="..."></a>

How can I remove all img tags with their parent a if the img contain this string?

style="display: none;"

Upvotes: 0

Views: 358

Answers (2)

MarcoS
MarcoS

Reputation: 17721

Something like this...:

if ($html =~ /(<a href="#"><img style="display:\s*none;".*?<\/a>)/g) {
    remove($1);
}

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39385

$html =~ s|<a\s+href[^>]*>\s*<img[^>]*style="display: none;"[^>]*>\s*</a>||g

This is checking the img tag inside the a tag. And also checking whether the given style attribute resides inside the img or not.

Upvotes: 1

Related Questions