Jump to content

Find and Replace all URL contianing jpeg/gif/png etc extension With image Tag


gr8abbasi

Recommended Posts

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.

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-

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.

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-

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;
?>

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.