View Full Version : Preg Match/Regular expressions question.
iceflyin
08-08-2007, 11:22 PM
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?
Yeah that's because your splitting the whole instance of <img> out, here is an example:
<?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 "
iceflyin
08-10-2007, 09: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...
<?php
$data = "<a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=3180342\" id=\"ctl00_Main_ctl00_UserFriends1_FriendRepeater_ctl02_friendLink\">Jeff</a><br><a href=\"http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&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];
?>
try this
<?php
$image = "<img src=\"http://sumurl.com/picture.jpg\" />";
preg_match("<[a-zA-Z0-9_-]*.jpg>", $image, $match, PREG_OFFSET_CAPTURE, 1);
//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?
iceflyin
08-10-2007, 10: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';
preg_match_all($regex,$data,$match);
var_dump($match);
$array = $match[1];
echo "<br><br> $array[0]";
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.