Solved

Find and Replace image url  in string with IMG SRC

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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 look for a specific file type in a local or remote server directory using PHP.

762 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

19 Experts available now in Live!

Get 1:1 Help Now