473,245 Members | 1,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

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 6768
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
1
by: Daniel | last post by:
I've been searching the net and Google news groups for a preg_match expression that will return true on strings containing (uppercase and lowercase) characters of A-Z, 0-9 and for instance Swedish...
10
by: aaron | last post by:
I need some help with validating an email address. Right now, I am doing this: function sys_is_valid_email ($s) { if (preg_match ("/^.+@.+\..+$/", $s)) { return 1; } else { return 0; } }
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
0
by: awebguynow | last post by:
I ran across this code, and it kind of made me nervous: (as an email validator) if ( !preg_match("/.*\@.*\..*/", $_POST) | preg_match("/(\)/", $_POST) ) 1) from bitwise experience with "C",...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
3
by: Happy Face | last post by:
Hi, All, I encountered this strange problem while using function preg_match. The following is the php code. when I set the line: $text = str_repeat('*', 12500); preg_match will return 0 for...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
5
by: jeddiki | last post by:
Hi, I am wanting to do a simple extraction of the three key header elements from a web page namely these: Is the preg_match() function the best way to find them and put them into variables...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.