Page 1 of 1

Add default path to image with PHP

Posted: Wed Dec 03, 2008 8:14 am
by lovelf
Hi, I would like to add a default path to the "src" of an image with PHP.

To all images that are inside directory "screenshots" I want to add a piece of code before "screenshots".

This:

Code: Select all

<img src="/screenshots/screenshot.jpg">
Would get to look like

Code: Select all

<img src="http://www.website.com/screenshots/screenshot.jpg">
I need this because I am on an affiliate program and I am displaying multiple products on my website with PHP include.

They have specified the full path to everything except for the images they have in the screenshots folder so I get to display everything correctly on my website except for one image per page which is an important one that is in the screenshots folder.

I do not have access to edit the files and add the full path myself so I need to manipulate them considering I am displaying them with a PHP include.

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 9:22 am
by mintedjo
Something like this maybe?

Code: Select all

preg_replace("/(<img.*?src=\")(.*?\".*?>)/i","$1http://www.website.com$2",$probablyAChunkOfHTMLCode);

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 9:23 am
by jaoudestudios
Use...

Code: Select all

$fullPath = ""http://www.website.com/";
echo "<img src=\"".$fullPath."screenshots/screenshot.jpg\">";
Hope that helps :)

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 9:24 am
by jaoudestudios
I do not have access to edit the files and add the full path myself so I need to manipulate them considering I am displaying them with a PHP include.
Sorry I missed this bit :(

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 9:56 am
by pickle
If the images are the only thing in the "/screenshots/" directory, use str_replace() rather than preg_replace() - it'll be faster:

Code: Select all

$theEditedContents = str_replace('/screenshots/','http://www.website.com/screenshots/',$theContentsOfTheInclude);

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 10:34 am
by lovelf
pickle wrote:

Code: Select all

$theEditedContents = str_replace('/screenshots/','http://www.website.com/screenshots/',$theContentsOfTheInclude);
I do not know why it is not replacing "/screenshots/" with "http://www.website.com/screenshots/"

I get this message: "Warning: Division by zero"

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 10:55 am
by pickle
This code works for me:

Code: Select all

<?PHP
$myString = '<img src="/screenshots/screenshot.jpg">';
$myString = str_replace('/screenshots/','http://www.website.com/screenshots/',$myString);
echo $myString
?>

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 11:10 am
by lovelf
Yeah. It works, remember the value of myString should be an include.

Code: Select all

<?PHP
$myString = "include_once('file.php');";
$myString = str_replace('/screenshots/','www.templatehelp.com/screenshots/',$myString);
echo $myString;
?>
It displays what I need to be the inclusion as text and not the file with the replaced values.

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 11:14 am
by pickle
Wrap your call to include in ob_start()and ob_get_contents() & the content of your include can then be put in a variable, for use with str_replace()

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 11:26 am
by lovelf
It worked pickle.
Thank you.

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 12:06 pm
by lovelf
How could I replace a second thing?

Code: Select all

return (str_replace('src="/screenshots/', 'src="http://www.website.com/screenshots/', $buffer));
That is for one thing.

If I wanted to replace another besides that one what would the code be?

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 12:11 pm
by pickle
Look at the documentation for str_replace() - you can use arrays.

Also, please post your PHP code in [ code=php ][ /code ] tags so we get syntax highlighting.

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 12:37 pm
by lovelf
OK, but what I actually need is to do a replace for something different than "/screenshots" now.

There is actually only one "/screenshots" that needs to be replaced so there is no need for the arrays in that case.

I need to invoke the str_replace twice, one for "/screenshots" and another to replace something different, with this code:

Code: Select all

{
  return (str_replace('src="/screenshots/', 'src="http://www.website.com/screenshots/',  $buffer));
  return (str_replace('different.php', 'http://www.website.com/different.php', $buffer));
}
I do not get the second str_replace to function, the first one still works.

Re: Add default path to image with PHP

Posted: Wed Dec 03, 2008 12:45 pm
by pickle
That's because you're returning the first result of str_replace(), so the second call never happens. Rather than returning the first str_replace(), store it back into $buffer.

You can use arrays for this - build one array of values to search and the other array of values to replace with.