knowj Posted September 10, 2008 Share Posted September 10, 2008 I need to retive a url in the following format frorm an xml file: http://farm4.static.flickr.com/3028/2808182892_e33d69ed02_m.jpg echo '<img src="'.$xml->entry[$i]->user->profile_image_url.'" alt="'.$xml->entry[$i]->title.'" title="'.$xml->entry[$i]->title.'"/> '; <content type="html"><p><a href="http://www.flickr.com/people/4353454@N4502/">user</a> has added a photo to the pool:</p> <p><a href="http://www.flickr.com/photos/999999@343/2808182892/" title="Sunset"><img src="http://farm4.static.flickr.com/3028/2808122892_e33d52ed02_m.jpg" width="240" height="180" alt="Sunset" /></a></p> </content> This would have to be done with a regular expression on the http:// and closing on the .jpg. I also cant work out which function willl be best to use ideally it would return the value rather than store it within an array. thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/123603-solved-getting-a-url-from-a-string/ Share on other sites More sharing options...
Psycho Posted September 10, 2008 Share Posted September 10, 2008 Well, it will return the result as an array, but that's a non issue.The question is if there will only be one match or multiple. If on you can use preg_match, if multiple you will want to use preg_match_all() $input = '<content type="html"><p><a href="http://www.flickr.com/people/4353454@N4502/">user</a> has added a photo to the pool:</p> <p><a href="http://www.flickr.com/photos/999999@343/2808182892/" title="Sunset"><img src="http://farm4.static.flickr.com/3028/2808122892_e33d52ed02_m.jpg" width="240" height="180" alt="Sunset" /></a></p> </content>'; preg_match('/http:\/\/[^ ]*?jpg/', $input, $matches); $found = $matches[0]; echo $found; Quote Link to comment https://forums.phpfreaks.com/topic/123603-solved-getting-a-url-from-a-string/#findComment-638327 Share on other sites More sharing options...
knowj Posted September 10, 2008 Author Share Posted September 10, 2008 thats great thanks. It's been a while since i touched regex i get rusty quickly. Quote Link to comment https://forums.phpfreaks.com/topic/123603-solved-getting-a-url-from-a-string/#findComment-638334 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.