August 20th, 2010, 12:19 PM
I'm trying to get the attributes from an img tag passed as a string to php.
Say I want to get the src attribute value I would do something like this:
However $src is returning :
$src = preg_replace("/^.*src=([\"'])(.+)\\1.*$/i","$2",$img);
http://www.domain.com/folder/file.jpg" width="50" height="50" alt="Dog picture"
Ideally I would like to place a negative backreference inside the second parenthesis like so :
but this fails!
$src = preg_replace("/^.*src=([\"'])([^\\1]+)\\1.*$/i","$2",$img);
Does anyone have any ideas?
August 21st, 2010, 12:02 AM
Ideally you wouldn't try to suck up every single character and then backtrack to forget the ones you didn't want.
August 23rd, 2010, 11:36 AM
Thanks requinix, I like your solution.
I haven't used lazy-quantifiers before, primarily because I didn't understand them, but your solution made me take a second look at my book.
Is this comment referencing the .* before and after the match I am trying to make? Is there another option to returning only the match without having to deal with arrays? Maybe returning and search through the array is faster and/or uses less resources than the regex I have?
I have come across an interesting blog about lazy-quantifiers. I'm not certain how PHP's regex engine deals with them, but it's some food for thought http://blog.stevenlevithan.com/archi...zy-performance
Last edited by aconway; August 23rd, 2010 at 01:06 PM.
August 23rd, 2010, 02:59 PM
- In essence, greedy quantifiers suck up "as much as they can" while lazy quantifiers suck up "as little as they can". Either way the engine always tries to make a match, and will jump through rings of fire to get it.
- Yes, the comment was aimed at using a greedy *. Most of the time a .* is used to match "a bunch of stuff", but it has a more sinister meaning that most people don't think about - or know. In most of those circumstances a .*? is better because it actually represents what's expect.
- If you're interested, I suggest reading a book on regular expressions. Most of them will also teach about how the engines themselves work, and with that knowledge alone you can write better expressions. It also, of course, is great stuff to know for troubleshooting.