gr8abbasi Posted May 3, 2010 Share Posted May 3, 2010 Hi, First of all thanks all of you. Actually i m trying to find and replace the URL that contains any img extension e.g jpg/gif/png ect in it and would lik to replace the URL with html IMAGE tag. Any Help will be highly appreciated. Thanks a lot in advance. Quote Link to comment https://forums.phpfreaks.com/topic/200524-find-and-replace-all-url-contianing-jpeggifpng-etc-extension-with-image-tag/ Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 First - where are we getting the urls from? an array? Assuming a simple string, and assuming standard image urls, use substr to check the last 4 characters for an extension. $url = "http://sometest.web.com/folder%4d/test-image.png"; if(substr($url,-5) == (".jpeg") || substr($url,-4) == ".jpg" || substr($url,-4) == ".gif" || substr($url,-4) == ".png"){ echo('<img src="'.$url.'" />'); } -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200524-find-and-replace-all-url-contianing-jpeggifpng-etc-extension-with-image-tag/#findComment-1052327 Share on other sites More sharing options...
gr8abbasi Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks for your reply. but i think i didnt explain correctly. i required first to find the URL in contents if any then i will go for this process you provided. But first i want to be able to find all the urls in contents and they are not the known URL so i cant give the length or anything that fix for on type of URL . So hopefully i explain well now. Waiting for your suggestion thanks. Quote Link to comment https://forums.phpfreaks.com/topic/200524-find-and-replace-all-url-contianing-jpeggifpng-etc-extension-with-image-tag/#findComment-1052344 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 Lol not really - what content? Text file? webpage? xml? database? You can pick out url's from any content using preg_match_all(). But depending on the content thats surrounding these urls, and any urls you would like to miss, the Pattern would change. -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200524-find-and-replace-all-url-contianing-jpeggifpng-etc-extension-with-image-tag/#findComment-1052351 Share on other sites More sharing options...
newbtophp Posted May 3, 2010 Share Posted May 3, 2010 Using ChemicalBliss's code you can do the following: <?php function callback($url){ $url = $url[1]; if (substr($url,-5) == (".jpeg") || substr($url,-4) == ".jpg" || substr($url,-4) == ".gif" || substr($url,-4) == ".png"){ return '<img src="'.$url.'" />'; } else return $url; } $content = "blah blah blah http://sometest.web.com/folder%4d/test-image.png"; $content = preg_replace_callback("/(http:\/\/([^\/]+)[^\s]*)/", "callback", $content); echo $content; ?> Quote Link to comment https://forums.phpfreaks.com/topic/200524-find-and-replace-all-url-contianing-jpeggifpng-etc-extension-with-image-tag/#findComment-1052362 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.