Jump to content

Image without alt search


cyberfox

Recommended Posts

Hello, foulks ;)

 

I have ~500 files with ~3000 <img> tags and 1/3 of them doesn't contain alt attribute. I'm in a need of regexp that could find those tags. I'm searching all <img> tags with <[a-zA-Z]+(\s+[a-zA-Z]+\s*=\s*("([^"]*)"|'([^']*)'))*\s*/>. Could someone modify it so it would skip those that have alt tags?

 

Thanks a billion:)

Link to comment
https://forums.phpfreaks.com/topic/238557-image-without-alt-search/
Share on other sites

This is a SLOW RegEx as it uses lookaround (negative lookahead), and I would only use it to root out problems and not in a script that gets called all the time.

 

I'm assuming proper HTML formatting - all attributes are in double quotes, and there's no space between the attribute, operator and content.

 

<img src="blah" alt="blah" width="x" height="x"> WILL BE IGNORED

<img src = "blah" alt = "something"> WILL NOT BE IGNORED

 

Here's the regex

(<img(?:[^>](?!alt=))*+>)

 

And in English

 

(<img(?:[^>](?!alt=))*+>)

 

Match the regular expression below and capture its match into backreference number 1 «(<img(?:[^>](?!alt=))*+>)»

  Match the characters “<img” literally «<img»

  Match the regular expression below «(?:[^>](?!alt=))*+»

      Between zero and unlimited times, as many times as possible, without giving back (possessive) «*+»

      Match any character that is NOT a “>” «[^>]»

      Assert that it is impossible to match the regex below starting at this position (negative lookahead) «(?!alt=)»

        Match the characters “alt=” literally «alt=»

  Match the character “>” literally «>»

 

 

Let me know if there's anything you don't understand

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.