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.

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.