Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-08-2007, 11:22 PM   PM User | #1
iceflyin
Regular Coder

 
Join Date: Jul 2007
Posts: 135
Thanks: 1
Thanked 0 Times in 0 Posts
iceflyin is an unknown quantity at this point
Preg Match/Regular expressions question.

I'm trying to pull the image url out of this string:
<img src="http://theimagehost.com/theimage.jpg"></a>



preg_match("'<img src=(.*?)></a>'si", $finalArray[$plusOne], $out_imageinfo);
list($width, $height) = getimagesize("$out_imageinfo[0]");


Warning: getimagesize(<img src="http://theimagehost.com/theimage.jpg"></a>)

Obviously I'm getting the entire string... I just want the url. I have been trying to figure it out with: http://www.regular-expressions.info/, but no luck yet...


I'm trying to use...
preg_split("'<img src=(.*?)></a>'si", $finalArray[$plusOne], $out_imageinfo);

It won't do anything?

Last edited by iceflyin; 08-09-2007 at 12:09 AM..
iceflyin is offline   Reply With Quote
Old 08-09-2007, 12:55 AM   PM User | #2
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
Yeah that's because your splitting the whole instance of <img> out, here is an example:
Code:
<?php
$image = "<img src=\"picture.jpg\" />";
list($tag, $img, $tag2) = split("[\"]+", $image, 3);
echo $img;
?>
the output is picture.jpg and if it is in a directory it will show the directory aswell

what I did is to go through the string $image and split the string each time it found a "
usik is offline   Reply With Quote
Old 08-10-2007, 09:18 AM   PM User | #3
iceflyin
Regular Coder

 
Join Date: Jul 2007
Posts: 135
Thanks: 1
Thanked 0 Times in 0 Posts
iceflyin is an unknown quantity at this point
Actually, that doesn't help... Because I'm trying to strip it out of string that contains multiple URLSs of different types.

However, the string only contains one instance of: "jpg"


So, what I'm trying to do is more like: REGEX: "^<(http)[*?](.jpg)/si" - Try to find a string that starts with http, and ends in JPG, and doesn't contain any <'s.

But, I can't get it to work at all. I'm new to RegEX.

I can't get this to do anything other than echo ArrayArrayArrayArray.........

Code:
        preg_match_all("/'http'(.+?)'jpg'/", $image, $matches);
        $array = $matches[2];
        echo $matches[0];
        echo $matches[1];
        echo $matches[2];
        echo $array[0];
        echo $array[1];
        echo $array[2];
And, here is an actual string I'm trying to parse and image out of...
Code:
<?php
$data = "<a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=3180342\" id=\"ctl00_Main_ctl00_UserFriends1_FriendRepeater_ctl02_friendLink\">Jeff</a><br><a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&amp;friendid=3180342\" id=\"ctl00_Main_ctl00_UserFriends1_FriendRepeater_ctl02_friendImageLink\"><img src=\"http://a267.ac-images.myspacecdn.com/00220/66/23/220403266_s.jpg\">" ;
$regex = '^</http(.+?)jpg/si';
preg_match($regex,$data,$match);
var_dump($match);
echo $match[1];
?>

Last edited by iceflyin; 08-10-2007 at 09:35 AM..
iceflyin is offline   Reply With Quote
Old 08-10-2007, 10:00 AM   PM User | #4
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
try this
PHP Code:
<?php
$image 
"<img src=\"http://sumurl.com/picture.jpg\" />";
preg_match("<[a-zA-Z0-9_-]*.jpg>"$image$matchPREG_OFFSET_CAPTURE1);
//print_r($match);
$image_array $match[0];
$image_name $image_array[0];
echo 
$image_name;
?>
and if it is multiple urls in a string why dont u split them up then do a foreach loop and run them through the preg_match to extract the image name?
usik is offline   Reply With Quote
Old 08-10-2007, 10:01 AM   PM User | #5
usik
New Coder

 
Join Date: Aug 2007
Location: Wagga, Australia
Posts: 59
Thanks: 0
Thanked 1 Time in 1 Post
usik is on a distinguished road
srry i double posted
usik is offline   Reply With Quote
Old 08-10-2007, 10:03 AM   PM User | #6
iceflyin
Regular Coder

 
Join Date: Jul 2007
Posts: 135
Thanks: 1
Thanked 0 Times in 0 Posts
iceflyin is an unknown quantity at this point
I just got this to work, but it cuts out the "jpg" part, hows it doing that? Oh wait, Ic... Hum... Ill just append "jpg", that works...

Code:
$regex = '/<img src=\"([^\"]*)(jpg)/iU';
preg_match_all($regex,$data,$match);
var_dump($match);
 $array = $match[1];
echo "<br><br> $array[0]";

Last edited by iceflyin; 08-10-2007 at 10:11 AM..
iceflyin is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:07 AM.


Advertisement
Log in to turn off these ads.