phpreplacejoomlarelative-pathabsolute-path

replacing relative image src with absolute ones on joomla


I need to mail some html code stored in the database.

For the images to show as well, I need to replace the relative path with absolute ones.

I have a variable called $display, which holds the HTML.

to do it I try the fallowing:

$root=JURI::root();
 str_replace('src="/images', 'src="' . $root .'images', $display); 

var_dump($display);//for testing

$display seems to have not changed at all.

update 1

Ive' been advised to change the code to this:

$root=JURI::root();
$display = str_replace('src="/images', 'src="' . $root .'images', $display)

I have also tried

$display = str_replace('src="/images', 'src="' . $root .'images', $display);

But - both didnt work just yet :[

It changed

src="/images/joomla_black.gif"

to

src="http://images/joomla_black.gif"

without the full url :[

any advice?

UPDATE 2

added this:

echo('non fixed:');
var_dump($display);

$root=JURI::root();
$display = str_replace('src="/images', 'src="' . $root .'images', $display);
//fix images in display to absolute paths for mailing


echo('fixed:');
var_dump($display);

both display strings seem identical on the out put


Solution

  • str_replace will return value, it won't change the $display variable, so you need to do

    $display = str_replace('src="/images', 'src="' . $root .'images', $display);

    The other line you posted will return root domain in case joomla is in a subfolder, I don't think you need that