Jump to content

[SOLVED] Get images url from string


megafu

Recommended Posts

hi there and thanks for reading :)

 

well i have a "small" problem . i have a string with lots of stuff in there (including multiple images url's - the number of url's is NOT static) and i would like to extract them. how should i do that?

 

any suggestions?

 

any help apreciated . thanks . :)

Link to comment
https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/
Share on other sites

ok sorry if i was confusing it really wasnt my intention. ill try to explain myself better.

 

i have a php variable - $variable - in which i have a looot of text.

example:

 

$variable = 'hell yeah i have text and a image <img src="http://www.google.com/image.gif"/>. isnt that cool? and another image maybe <img src="http://www.yahoo.com/yahooimage.gif"/>'

 

and i want to "extract" the two urls of the images:

 

1 - http://www.google.com/image.gif

2 - http://www.yahoo.com/yahooimage.gif

 

PS: Notice that the number of images is not constant ... it maybe 1 or 10.

 

thanks for your help . :)

Here is the code that should work

 

<?php
$variable = "this is some text, avec url= http://www.google.com/image.gif and this http://www.yahoo.com/yahooimage.gif";
$words = substr_count($variable, " ");
$words = $words+1;
$word = explode(" ",$variable);
$num = 1;
for($i = 0;$i <= $words;$i++)
{
$var = trim($word[$i]);
if(eregi("^(http|https):\/\/.+(\.gif|\.jpg|\.jpeg|\.png)$",$var))
{
	echo "{$num}. {$var}<br />";
	$num++;
}
}
?>

 

This code counts the spaces then adds one for the amount of words. then is explodes the spaces and checks to make sure if the word matches the regex. $num is just so it will say 1. url 2.url etc

 

You can thank Sasa for the regex ;D

 

Hope this works

 

~ Chocopi

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.