Solved

Find and Replace image url  in string with IMG SRC

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.  …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now