image path replacement with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

image path replacement with php

Post by jimthunderbird »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi All, 

   Currently I have a file named test.html it looks like this:
[syntax="html"]   <html>
       <head>
       </head>
       <body>
             <table>
                    <tr>
                         <td>
                             <img src="../../logo.jpg"></img>
                         </td>
                         <td>
                             <img src="../../header.jpg"></img>
                         </td>
                    </tr>
             </table>
       </body>

   </html>
Is it possible to use php to:
read this file in, then replace the ../../ with mypath so the two images will look like <img src="mypath/logo.jpg"></img> and <img src="mypath/header.jpg"></img> while other code remains the same?

Are there any php scripts doing this type of task or anyone would help?

With my best,
Jim


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Well, it depends on the rest of the file. If there are no other places in this file with the string "../../", then you could simply use str_replace(), otherwise, you'd probably need to use a regex.

Now here is another thing to take into account, when you are talking about replacing those "../../", do you mean like opening the file, replace them with your path and then save the change once and forever, or you want to replace it on-the-fly (each time the file is executed)?
If it is the former that you want to do, then file_get_contents() and the like are your friends.
If the latter is what you want to do, then this article may be of interest: Output Buffering With PHP.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

you can try similar to the following, but it's not fool proof:

Code: Select all

$imgpath = realpath('../../image.jpg');
$imagpath = str_replace($_SERVER['DOCUMENT_ROOT'], 'http://' . $_SERVER['HTTP_HOST'], $imgpath);
User avatar
jimthunderbird
Forum Contributor
Posts: 147
Joined: Tue Jul 04, 2006 3:59 am
Location: San Francisco, CA

the regular expression

Post by jimthunderbird »

Hi All,
Thank you very much for your help. I guess I really need to go into regular expression to solve this problem. I will have an image folder storing an image named logo.jpg.
At file number 1, I will have a code like

Code: Select all

<img src="../../image/logo.jpg"></img>
At file number 2, I will have a code like

Code: Select all

<img src="../../../../image/logo.jpg"></img>
At file number 3, I will have a code like

Code: Select all

<img src="../../../image/logo.jpg"></img>
If I wish to replace the image path in all 3 files with

Code: Select all

<img src="mypath/image/logo.jpg"></img>
, what regular expression should I use? Also, should I use ereg_replace or preg_replace?

Thank you very much for you help.

With my best,
Jim
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_replace_callback() would be my vote.
Post Reply