I'm working on an email template system where my client will go to a page and copy & paste the source code into their crm system.

I'm trying to find a way to automatically replace the image path without the use of echo if possible.

I've figured this out already with this script:

<?
function GetFileDir($php_self){
$filename = explode("/", $php_self); // THIS WILL BREAK DOWN THE PATH INTO AN ARRAY
for( $i = 0; $i < (count($filename) - 1); ++$i ) {
$filename2 .= $filename[$i].'/';
}
return $filename2;
}
?>

But I don't wanna have to go in and put

<?php echo GetFileDir("http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']); ?>

before all the image paths.

So basically anywhere that src="images/whatever.jpg" is listed it will replace "images/" with "http://www.mydomain.com/images/whatever.jpg"

is it possible to do this without having to place code before "images" ?

    You can either consider using regular expressions to find all links that are not full URLs (e.g. they don't begin with '(protocol)://') and prepend whatever you'd like, or you can possibly use something like:

    <base href="http://www.mydomain.com/">

    in the header of the HTML document. I'm not sure if this is valid/understood as far as HTML e-mail messages go, so some testing/research may be needed to verify this works.

      What would I do with the base url? What that add the url to the beginning of "images/"

        See this W3Schools page for more info about the HTML <base> tag.

        EDIT: Hmm... after reading that page, they really don't give you much information at all, do they?

        The <base> tag will essentially tell the HTML renderer that any href/src attribute in the entire HTML document that isn't an absolute/full URL (e.g. it doesn't start with '(protocol)://') should be considered a path relative to the 'href' specified in the <base> tag.

        Full URLs, however, will be left unchanged (so you could make a link to 'http://www.phpbuilder.com/board/', for example, and the <base> tag wouldn't apply).

          Thanks!

          I just tried that, but unfortunately it doesn't actually put the rest of the url before the images source. So my source is still "images/myimage.jpg" instead of "www.mydomain.com/images/myimage.jpg"

          I think it could be done with PHP but I'm just at a loss. I've tried a number of things, but it all requires me to echo infront of the images path, which isn't what I'm trying to achieve here.

            What did you try? The W3Schools example (slightly modified):

            <html>
            <head>
            <base href="http://www.w3schools.com/images/" />
            </head>
            
            <body>
            <img src="stickman.gif" />
            </body>
            </html>

            worked for me.

              Personally what I do is in my config.php file I define a variable $siteurl = 'www.example.com/'; then i just do

               echo '<img src="'. $siteurl .'"images/image.jpg">';
              // or
              echo '<a href="'. $siteurl .'"path/to/page">Link</a>';
                bradgrafelman;10980543 wrote:

                What did you try? The W3Schools example (slightly modified):

                <html>
                <head>
                <base href="http://www.w3schools.com/images/" />
                </head>
                
                <body>
                <img src="stickman.gif" />
                </body>
                </html>

                worked for me.

                Yes that worked to display the image, but when you look at the source code the image source is still "stickman.gif" I would need the source to display "http://www.w3schools.com/images/stickman.gif" because the client is going to copy the source code & paste it into their crm email. Emails do not accept base href, I just tried it and it was a no go.

                  Well one thing you could do is use [man]preg_replace/man with a specially crafted regexp to match such attributes. Example:

                  // siteurl: Must begin with protocol (e.g. "http://") and end with "/"
                  $siteurl = 'http://www.w3schools.com/';
                  
                  $test = '<html>
                  <head>
                  <script src="foo/bar.script"></script>
                  </head>
                  
                  <body>
                  <img src= \'stickman.gif\' />
                  </body>
                  </html>';
                  
                  $pattern = '@((?:href|src)\s*=\s*(["\']))((?![a-z0-9]+://).+?)(?=\2)@i';
                  $replace = "$1$siteurl$3";
                  
                  $test = preg_replace($pattern, $replace, $test);
                  echo $test;

                  However, now that I understand what you're trying to do a little better, I wonder if it wouldn't be better to use [man]DOM[/man] to manipulate the HTML instead.

                    Write a Reply...