Solved

Find and Replace image url  in string with IMG SRC

Posted on 2006-06-17
7
421 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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