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.

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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-

Link to comment
Share on other sites

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

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.