Jump to content

[SOLVED] Matching where something is NOT found


lemmin

Recommended Posts

With this string:

<?php
"<img src=\"1.jpg\" /><img src=\"2.jpg\" alt=\"2\" />"
?>

 

I am trying to add a string like "alt=\"1\"" to only the img tag that does NOT already have an alt property

 

This regex matches the src property just like I want it to:

"/(<\s*img\s*src\s*=\s*[\"|'](.+?)[\"|'])/"

 

But when I try to match the end tag without any other characters, it gets greedy and matches the quote from the end of the alt property:

"/(<\s*img\s*src\s*=\s*[\"|'](.+?)[\"|'])\s*\/>/"

 

So, is there a way to implement this regex where the .+? won't match the middle quotes?

 

Or, what is a good way to match the img tag that doesn't have an alt property?

 

I know I could do it with two statements, but it seems like it should be possible with just one.

 

Thanks.

Link to comment
Share on other sites

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.