Page 1 of 1

image path replacement with php

Posted: Mon Aug 21, 2006 3:23 am
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]

Posted: Mon Aug 21, 2006 4:13 am
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.

Posted: Mon Aug 21, 2006 4:41 am
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);

the regular expression

Posted: Mon Aug 21, 2006 7:16 pm
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

Posted: Mon Aug 21, 2006 7:47 pm
by feyd
preg_replace_callback() would be my vote.