Reputation: 3859
I have an example string as follows
$string = '
http://image.gsfc.nasa.gov/image/image_launch_a5.jpg
http://pierre.chachatelier.fr/programmation/images/mozodojo-original-image.jpg
http://image.gsfc.nasa.gov/image/image_launch_a5.jpg
Alot of text
http://www.google.com/intl/en_ALL/images/logos/images_logo_lg.gif
more text';
I want to be able to extraxt the url's of the first three images (basicly whatever # of images are at the start of the string) but not extract any image URLs once my non image text starts. I can successfully use regex to grab all the image URL, but it also grabs the last google.com image which is inside the text.
Thanks for any ideas!!
Upvotes: 1
Views: 167
Reputation: 101614
How about avoiding regex and using explode
instead?
$string = '....';
$urls = array();
$lines = explode(PHP_EOL,$string);
foreach ($lines as $line){
$line = trim($line);
// ignore empty lines
if (strlen($line) === 0) continue;
$pUrl = parse_url($line);
// non-valid URLs don't count
if ($pUrl === false) break;
// also skip URLs that aren't images
if (stripos($pUrl['path'],'.jpg') !== (strlen($pUrl['path']) - 4)) break;
// anything left is a valid URL and an image
// also, because a non-url fails and we skip empty lines, the first line
// that isn't an image will break the loop, thus stopping the capture
$urls[] = $line;
}
var_dump($urls);
Example at IDEOne
Upvotes: 1
Reputation: 794
Let R be regex to grab an image url
You need to grab (R)+ , i.e 0 or more occurrences of R
or mostly ((R)(w)?)+
Where w represents a regular expression to match white spaces.
Upvotes: 2