468,247 Members | 1,317 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,247 developers. It's quick & easy.

preg_match help - stripping img tag - help

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.

Jul 17 '05 #1
2 6627
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
Jul 17 '05 #2
you rock - that worked thx!

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by fartsniff | last post: by
1 post views Thread by Daniel | last post: by
10 posts views Thread by aaron | last post: by
reply views Thread by awebguynow | last post: by
5 posts views Thread by Mark Woodward | last post: by
3 posts views Thread by Happy Face | last post: by
8 posts views Thread by Thomas Mlynarczyk | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.