Solved

Find and Replace image url  in string with IMG SRC

Posted on 2006-06-17
7
424 Views
Last Modified: 2008-02-01
Hi! Experts!

I have some string like
$str = "abcdefg http://www.blahblah.com/pic1.gif hijklmnop http://www.blahblah.com/pic2.gif qrstuvwxyz";

And when i query this from MySQL db and show to webpage, i want the image url (http://www.blahblah.com/pic1.gif and http://www.blahblah.com/pic2.gif) to show as an image (with <IMG SRC tag)

-----------------------------------
Can someone give me an Idea

0
Comment
Question by:xtrue
  • 2
  • 2
7 Comments
 

Expert Comment

by:iphone
ID: 16925947
you can do something like this:

$str = "http://www.blahblah.com/pic1.gif|http://www.blahblah.com/pic2.gif";

then use :

$url = explode("|",$str);

read with:

$url[0], $url[1]

$url[0] with have value of http://www.blahblah.com/pic1.gif where as $url[1] will have http://www.blahblah.com/pic2.gif";

0
 

Author Comment

by:xtrue
ID: 16925963
Nop... sorry for my unclear question....

I mean it should display like

abcdefg [PIC1] hijklmnop [PIC2] qrstuvwxyz

which [PIC1] is the picture of  http://www.blahblah.com/pic1.gif  
and    [PIC2] is the picture of http://www.blahblah.com/pic2.gif
Show both pictures as picture not just link.

Thanks again
0
 

Expert Comment

by:iphone
ID: 16925984
if the $str is contruct by yourself you can actually put in html tag:

$str = "abcdefg <img src='http://www.blahblah.com/pic1.gif'> hijklmnop <img src='http://www.blahblah.com/pic2.gif'> qrstuvwxyz";

when you do

echo $str;

you will see the result that you want.

0
 

Author Comment

by:xtrue
ID: 16925995
Thanks...

But it like a webboard with picture.. when use wish to show picture they just put the message and follow by http://www.blahblah.com/pic1.gif  not to <img src="http://www.blahblah.com/pic1.gif ">

When user wish to view their result just show page and this should display picture.





0
 
LVL 37

Accepted Solution

by:
Harisha M G earned 125 total points
ID: 16927305
Hi, try this:

<?php
$str = "abcdefg http://www.blahblah.com/pic1.gif hijklmnop http://www.blahblah.com/pic2.gif qrstuvwxyz";

$replaced =  preg_replace("#\s((www|http:).*?(jpeg|jpg|gif|bmp|png))\s#is"," <img src=\"$1\"> ",$str);

echo $replaced;
?>


---
Harish
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question