[Solved] Strip Image Addresses from HTML

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

[Solved] Strip Image Addresses from HTML

Post 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.
Last edited by Grim... on Tue Aug 10, 2004 3:20 am, edited 1 time in total.
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

untested

Code: Select all

#<\s*img&#1111;^>]*?src\s*=\s*(&#1111;''"]?)(.*?)\\1&#1111;^>]*?>#is
note: this is intended for preg_match_all
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post 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 ']'
:|
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

sorry.. it was also intended to be inside a single quote string ;)
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Spot on.
Cheers, Feyd.
Post Reply