Forum Moderators: coopster

Message Too Old, No Replies

Regex to get img src urls

         

planbeta

4:50 pm on Jul 25, 2003 (gmt 0)

10+ Year Member


Hello!

I have been trying to create a regexp pattern so I can get the src= part of any image tag. This is what I have come up with so far:

$matchSrc = '/<\s*IMG\s*\S*src=\s*[\"¦\'](.*?)\s*[\"¦\']/i';

This works fine, but how can I modify this so to collect images that aren't in "" or '' (I know they SHOULD be surround by quotes, but I'm finding a lot that aren't).

Thank you

Chris

vincevincevince

6:05 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"/\< *[Ii][Mm][Gg] *[Ss][Rr][Cc]= *[\"¦\']{0,1}([^\"\'\>]*)/"

brotherhood of LAN

8:21 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



"/\< *[Ii][Mm][Gg] *[Ss][Rr][Cc]= *[\"¦\']{0,1}([^\"\'\>]*)/"

You can use the pattern modifier "i" so that the regex matches are not case sensitive, so you could change it to

"/\< *[img] *[src]= *[\"\']{0,1}([^\"\'\>]*)/i"

You should maybe make provision for erroneous spaces in the tag, AFAIK spaces can appear in parts of the regex and is still standard HTML.

tschild

9:12 pm on Jul 25, 2003 (gmt 0)

10+ Year Member



These regexes of course still don't match the following tags:

<img src="whatever" width="...
<img width="... " heigth=".." src="whatever" ...
<img border="0" src="whatever" ...

vincevincevince

9:21 pm on Jul 25, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



tschild, thanks for pointing that out :p

"/\< *[img][^\>]*[src] *= *[\"\']{0,1}([^\"\'\ >]*)/i"

planbeta

11:58 am on Jul 26, 2003 (gmt 0)

10+ Year Member



Cheers for all the replies/help :)

It's working great now - I always wondered what the /i did , now I know!

Thanks

BTW is it possible to optimise/compress images on the fly (both jpeg/gif) in order to reduce file size?

Chris

vincevincevince

12:04 pm on Jul 26, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes, it is possible - the key functions to use, in this order, are:
$_FILES['photo']['tmp_name']
fopen()
fread()
imagesx()
imagesy()
imagecreatetruecolor()
imagecopyresized()
ob_start()
imageJPEG()
ob_get_contents()
ob_end_clean()

planbeta

12:10 pm on Jul 26, 2003 (gmt 0)

10+ Year Member



Cheers mate!

Chris