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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

try

<?php
$variable = "this is some text, avec url= http://www.google.com/image.gif and this http://www.yahoo.com/yahooimage.gif";
preg_match_all('/(http|https):\/\/[^ ]+(\.gif|\.jpg|\.jpeg|\.png)/',$variable, $out);
print_r($out[0]);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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