tg****@gmail.com wrote in news:1113963237.594139.263190
@l41g2000cwc.googlegroups.com:
I need help with a regex. Been at it for hours and need some sanity
help :>
I am using preg_match in PHP to strip a IMG html tag. I want to strip
any IMG tag that has /work/doc?g= in the SRC attribute.
The basic one i wrote works but obviously doesnt work when you add the
height/width attribute into it:
"<img src=\"\/work\/doc\?g\=([A-Z]+)\">"
some possible variations i am encountering:
<img height="42" src="/work/doc?g=ABCDEFG" width="234">
<img style="WIDTH: 258px; HEIGHT: 46px" src="/work/doc?g=HIJKLMNOP">
All I want is the ABCDEFG or HIJKLMNOP part.
Can someone write the regex that works? Any help is greatly
appreciated.
Try this one:
preg_match("/<img(.*)src=\"\/work\/doc\?g\=(.*?)\"(.*)>/i", $string1,
$matches);
Not too pretty but it works with $string1 set to both of your example
variations. $matches[0] contains the entire img tag, and $matches[2] will
hold just the ABCDEFG part. $matches[1] and $matches[3] will vary,
depending upon whether or not there were attributes before or after the
src attribute inside the img tag.
hth
--
Bulworth : PHP/MySQL/Unix | Email : str_rot13('f@fung.arg');
--------------------------|---------------------------------
<http://www.phplabs.com/> | PHP scripts, webmaster resources