Add default path to image 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
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Add default path to image with PHP

Post 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.
mintedjo
Forum Contributor
Posts: 153
Joined: Wed Nov 19, 2008 6:23 am

Re: Add default path to image with PHP

Post by mintedjo »

Something like this maybe?

Code: Select all

preg_replace("/(<img.*?src=\")(.*?\".*?>)/i","$1http://www.website.com$2",$probablyAChunkOfHTMLCode);
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Add default path to image with PHP

Post by jaoudestudios »

Use...

Code: Select all

$fullPath = ""http://www.website.com/";
echo "<img src=\"".$fullPath."screenshots/screenshot.jpg\">";
Hope that helps :)
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Add default path to image with PHP

Post 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 :(
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add default path to image with PHP

Post 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);
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Add default path to image with PHP

Post 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"
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add default path to image with PHP

Post 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
?>
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Add default path to image with PHP

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add default path to image with PHP

Post 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()
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Add default path to image with PHP

Post by lovelf »

It worked pickle.
Thank you.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Add default path to image with PHP

Post 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?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add default path to image with PHP

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Add default path to image with PHP

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Add default path to image with PHP

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply