Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.