Solved

Find and Replace image url  in string with IMG SRC

Posted on 2006-06-17
7
426 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
reverse engineer .sql from php files 11 52
Row insertion failed. Array 5 61
Change a button after e-mail 4 20
Why is my implode function empty 8 13
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 …
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

756 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