megafu Posted August 17, 2007 Share Posted August 17, 2007 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 . Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/ Share on other sites More sharing options...
nathanmaxsonadil Posted August 17, 2007 Share Posted August 17, 2007 I dont understand what you mean do you have a url like http://myurl.com/index.php?theimage=img02&theimage3=img03 ? Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/#findComment-326898 Share on other sites More sharing options...
megafu Posted August 17, 2007 Author Share Posted August 17, 2007 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 . Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/#findComment-326916 Share on other sites More sharing options...
chocopi Posted August 17, 2007 Share Posted August 17, 2007 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 Hope this works ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/#findComment-327075 Share on other sites More sharing options...
sasa Posted August 17, 2007 Share Posted August 17, 2007 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]); ?> Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/#findComment-327119 Share on other sites More sharing options...
megafu Posted August 18, 2007 Author Share Posted August 18, 2007 thanks yall for your precious help . it worked perfectly . i had thought of a similar solution but i am a TOTAL noob with regular expressions . anyway ... thanks a looooot! Quote Link to comment https://forums.phpfreaks.com/topic/65469-solved-get-images-url-from-string/#findComment-327240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.