View Full Version : Preg Match/Regular expressions question.

08-09-2007, 12:22 AM
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?

08-09-2007, 01:55 AM
Yeah that's because your splitting the whole instance of <img> out, here is an example:

$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 "

08-10-2007, 10:18 AM
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.........

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...

$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';
echo $match[1];

08-10-2007, 11:00 AM
try this

$image = "<img src=\"http://sumurl.com/picture.jpg\" />";
preg_match("<[a-zA-Z0-9_-]*.jpg>", $image, $match, PREG_OFFSET_CAPTURE, 1);
$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?

08-10-2007, 11:01 AM
srry i double posted

08-10-2007, 11:03 AM
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...

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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum