Page 1 of 1

[Solved] Strip Image Addresses from HTML

Posted: Mon Aug 09, 2004 11:25 am
by Grim...
I've making a function for the footer file of my PHP pages that tells me how big the page was in KB.

I've got the (HTML) contents of the page in a string called $contents and I'd like to be able to take all the bits ending in .jpg" or .gif" and get the addresses for each of them.

For example, here is a small HTML page:

Code: Select all

<html>
<head>
<title>Bob</title>
</head>
<body>
<img src="images/bob.jpg">
<br><br>
<img src="images/hector.gif" alt="Hector Rocks">
</body>
</html>
Can anyone who can use preg_match (or whatever) knock me up a quick function that will take that code above stick the image addresses into an array, like:

Code: Select all

1 => images/bob.jpg
2 => images/hector.gif
Any help much appreciated.

Posted: Mon Aug 09, 2004 11:26 am
by Grim...
I suck a preg_ things, in case you were wondering why I couldn't do it myself :)
I have done it, but using stristr() and explode(), so I'm sure there's a faster way!

Posted: Mon Aug 09, 2004 11:31 am
by feyd
untested

Code: Select all

#<\s*img&#1111;^>]*?src\s*=\s*(&#1111;''"]?)(.*?)\\1&#1111;^>]*?>#is
note: this is intended for preg_match_all

Posted: Mon Aug 09, 2004 11:58 am
by Grim...
If I go for:

Code: Select all

preg_match_all("#<\s*img&#1111;^>]*?src\s*=\s*(&#1111;''"]?)(.*?)\\1&#1111;^>]*?>#is", $content, $img, PREG_PATTERN_ORDER);
I get
Parse error: parse error, unexpected ']'
:|

Posted: Mon Aug 09, 2004 12:06 pm
by feyd
sorry.. it was also intended to be inside a single quote string ;)

Posted: Tue Aug 10, 2004 3:19 am
by Grim...
Spot on.
Cheers, Feyd.