View Full Version : PHP Regex
mrnightowl
10-12-2007, 04:41 PM
Having a small problem with regex that I thought was working properly. Trying to match just the img src value ie. http://www.site.com/pic.jpg ...Currently using this regex in my pregmatch
<\s*img [^\>]*src=\"([^\">]+)/is",$buff,$match);
I want just the source no matter what follows src or is before src
Like this for example:
<img src="http://www.site.com/pic.jpg" style="border-width:0px;">
<img border="0" alt="" src="http://www.site.com/pic.jpg">
Using same regex I want to return only the src url. I'm missing something simple in my regex but I can't seem to see it..
kbluhm
10-12-2007, 06:56 PM
Give this a shot:
'/\<img.+src="([^"]+)"[^\>]*\>/Usi'
mrnightowl
10-12-2007, 07:13 PM
Give this a shot:
'/\<img.+src="([^"]+)"[^\>]*\>/Usi'
no go...unexpected T here I'll post the preg match to give an idea maybe.. dunno stumping me why it works with some but not others...
preg_match("/<a id=\"images\" href=\"http:\/\/somesite.com\">.*?<\s*img [^\>]*src=\"([^\">]+)/is",$buff,$match);
$image = $match[1]
kbluhm
10-12-2007, 09:04 PM
That regex looks nothing like what I posted.
mrnightowl
10-12-2007, 09:20 PM
That regex looks nothing like what I posted.
crap sorry didn't realize i did that so much stuff open...
preg_match("/<a id=\"images\" href=\"http:\/\/somesite.com\">'/\<img.+src="([^"]+)"[^\>]*\>/Usi',$buff,$match);
Producing: Parse error: syntax error, unexpected '('
If I correct the unexpected ( is when I get the other error
Inigoesdr
10-12-2007, 09:29 PM
It could possibly have something to do with that anchor tag, and you mixing the single/double quotes up.
mrnightowl
10-12-2007, 10:04 PM
Ok so using that regex and after fixing the unexpected variables and t string.. I ran it like this:
preg_match("/<a id=\"images\" href=\"http:\/\/somesite.com\">'.*?<img.+src=\"([^\"]+)[^\>]*>/Uis",$buff,$match);
With no luck no errors but not giving just source on tags that have border and alt attributes it returns <img border="0" alt="" src="http://www......
Those img files with just <img src=".... it does return just the URL fine same as before with the method I originally used
mrnightowl
10-14-2007, 07:15 PM
Got this all squared away the other day... but I wanted to share a great REGEX tool link that I found in the process. Works awesome for figuring out your problems. Thanks all... laters
http://www.dhtmlgoodies.com/scripts/regular-expression/regular-expression.html
hardnrg
10-15-2007, 04:43 AM
I use this Firefox Addon to quickly test regular expressions;
https://addons.mozilla.org/en-US/firefox/addon/2077
mrnightowl
10-15-2007, 09:24 AM
I use this Firefox Addon to quickly test regular expressions;
https://addons.mozilla.org/en-US/firefox/addon/2077
Aaah very nice... added it to my list.
I see regular expression based questions come up on here alot... good to have some resources to mark down for future issues.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.