Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 03-11-2006, 03:16 PM   #1
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
Question for php and regex gurus...

I need to parse html for any image tags and store all of them in an array...

So if this is the html:

blah blah <img src="asdf.jpg" width="150"> more blah <img border="1" src="kasdf.gif">

The array should be:

$images[0] = asdf.jpg
$images[1] =kasdf.gif

Anybody know how I can do this?
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 03:25 PM   #2
mortenb
Confirmed User
 
mortenb's Avatar
 
Join Date: Jul 2004
Location: Denmark ICQ: 7880009
Posts: 2,203
I'm thinking something similar to this:

Code:
preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)\"?'?`?|i", $string, $matches);
mortenb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 04:04 PM   #3
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
Quote:
Originally Posted by mortenb
I'm thinking something similar to this:

Code:
preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)\"?'?`?|i", $string, $matches);

That works great. Thank you!
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 04:06 PM   #4
Why
MFBA
 
Industry Role:
Join Date: Mar 2003
Location: PNW
Posts: 7,230
there are things other then images that use src= so you might want to be more explicit with that regex
Why is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 04:13 PM   #5
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
Quote:
Originally Posted by Why
there are things other then images that use src= so you might want to be more explicit with that regex

Good point. It matches anything after src even if it's not in an img tag. Any ideas?
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 04:38 PM   #6
Why
MFBA
 
Industry Role:
Join Date: Mar 2003
Location: PNW
Posts: 7,230
Code:
preg_match_all("|src\=\"?'?`?([[:alnum:]:?=&@/._+-]+)+\.(gif|GIF|jpg|JPG)\"?'?`?|i", $string, $matches);
might work. but i didnt test it. it would only check for gifs and jpegs though.

Last edited by Why; 03-11-2006 at 04:39 PM.. Reason: php to code
Why is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 08:33 PM   #7
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
Thanks for the help. What can I use to match just the image name from the string extracted from the first match?

Ie. How do I match just pic.jpg out of: src="http://example.com/images/pic.jpg"

I tried: preg_match("/\/.+?\.jpg/", $matches[1], $imagematch);
but that matches the longest string possible (//example.com/images/pic.jpg)

Thanks.
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 08:36 PM   #8
psili
Confirmed User
 
Join Date: Apr 2003
Location: Loveland, CO
Posts: 5,526
Quote:
Originally Posted by BigBen
Thanks for the help. What can I use to match just the image name from the string extracted from the first match?

Ie. How do I match just pic.jpg out of: src="http://example.com/images/pic.jpg"

I tried: preg_match("/\/.+?\.jpg/", $matches[1], $imagematch);
but that matches the longest string possible (//example.com/images/pic.jpg)

Thanks.
Maybe do a greedy match at first, just to get all the full & relative paths to the actual images grabbed out of the src tags. Then implode that $matched array into a string. Against that string, do a more refined match to just grab out the image names.
__________________
Your post count means nothing.
psili is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 03-11-2006, 08:45 PM   #9
BigBen
Confirmed User
 
Join Date: Nov 2004
Location: scv
Posts: 2,299
Quote:
Originally Posted by psili
Maybe do a greedy match at first, just to get all the full & relative paths to the actual images grabbed out of the src tags. Then implode that $matched array into a string. Against that string, do a more refined match to just grab out the image names.
I'm having trouble doing that second part. My first regex will store the full path and I need to do a match against that string for the single image.

Last edited by BigBen; 03-11-2006 at 08:47 PM..
BigBen is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.