php - Issue replacing forward slash in string

admin2025-06-02  3

I have a very weird issue trying to do a simple str_replace in php in a wordpress application.

I am trying to create a path to an image using wordpress functions to get the site path and the resource path. Below are 3 methods that I tried, all giving results that I don't understand.

Original method: (This worked on my own localhost but when in prod, the admin side gives a different $fullpath to the front end side)

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

Output on Front End was a $fullpath of

string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

But when in Admin End was (Note the double forward slash)

string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

I noticed that on the production server $attachment->image_src_large[0] didn't include the domain as it had on the dev server. So with this in mind, and in an attempt to solve the double slash on the Admin End, I tried numerous methods as detailed below - This is when things started to get a bit weirder:

2.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = str_replace("//", "/", $homepath.$filepath);

//var_dumps Give
//FRONT END
//$attachment->image_src_large[0] - string(89) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 

//ADMIN (Gave http instead of https)
//$attachment->image_src_large[0] - string(84) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(109) "/usr/www/users/currentuser/http:/currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace('/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

//var_dumps Give
//$attachment->image_src_large[0] - string(51) "/wp-content/uploads/2017/10/image1-1024x684.jpg"
//$filepath - NULL

3.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = $homepath.$filepath;
$finalpath = str_replace("//", "/", $fullpath);

$fullpath - string(115) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
$finalpath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg"

The weird thing here was the size of the $finalpath string vs $fullpath sting, it looks like the str_replace works but when php reads the string it replaces / with the domain

4. Tried escaping the forward slash to be inserted

...
$finalpath = str_replace("//", "\/", $fullpath);

//$fullpath - string(110) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
//$finalpath - string(110) "/usr/www/users/currentuser/http:\/currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg" 

I can't figure this one out! Anybody able to help, I would greatly appreciate it!

I have a very weird issue trying to do a simple str_replace in php in a wordpress application.

I am trying to create a path to an image using wordpress functions to get the site path and the resource path. Below are 3 methods that I tried, all giving results that I don't understand.

Original method: (This worked on my own localhost but when in prod, the admin side gives a different $fullpath to the front end side)

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

Output on Front End was a $fullpath of

string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

But when in Admin End was (Note the double forward slash)

string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

I noticed that on the production server $attachment->image_src_large[0] didn't include the domain as it had on the dev server. So with this in mind, and in an attempt to solve the double slash on the Admin End, I tried numerous methods as detailed below - This is when things started to get a bit weirder:

2.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = str_replace("//", "/", $homepath.$filepath);

//var_dumps Give
//FRONT END
//$attachment->image_src_large[0] - string(89) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 

//ADMIN (Gave http instead of https)
//$attachment->image_src_large[0] - string(84) "/wp-content/uploads/2017/10/image1-2962-1024x684.jpg" 
//$filepath - string(109) "/usr/www/users/currentuser/http:/currentdomain.co.za/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace('/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;

//var_dumps Give
//$attachment->image_src_large[0] - string(51) "/wp-content/uploads/2017/10/image1-1024x684.jpg"
//$filepath - NULL

3.

$site_url = get_site_url();
$homepath = get_home_path();
$filepath = $attachment->image_src_large[0];
$fullpath = $homepath.$filepath;
$finalpath = str_replace("//", "/", $fullpath);

$fullpath - string(115) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
$finalpath - string(114) "/usr/www/users/currentuser/https:/www.currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg"

The weird thing here was the size of the $finalpath string vs $fullpath sting, it looks like the str_replace works but when php reads the string it replaces / with the domain

4. Tried escaping the forward slash to be inserted

...
$finalpath = str_replace("//", "\/", $fullpath);

//$fullpath - string(110) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image-1-1024x684.jpg" 
//$finalpath - string(110) "/usr/www/users/currentuser/http:\/currentdomain.co.za/wp-content/uploads/2017/10/image-1-1024x684.jpg" 

I can't figure this one out! Anybody able to help, I would greatly appreciate it!

Share Improve this question asked Feb 12, 2018 at 12:13 BluekableBluekable 111 silver badge3 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I figured out a solution! Not that it explains some of the weird str_replace activity that I was seeing, so if anyone can explain that please do.

But to get around the original issue I was experiencing - The line

$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);

was only having an effect on either back or front end but not both because $attachment->image_src_large[0] was different for each. On front end I would get https://... but on back end, for whatever reason, I would get http://....

So I expanded $site_url = get_site_url(); to be:

$site_url = get_site_url();
  if (strpos($site_url, 'https') !== false){
    $ssl_site_url = $site_url;
    $plain_site_url = str_replace("https", "http", $site_url);
    } else {
    $plain_site_url = $site_url;
    $ssl_site_url = str_replace("http", "https", $site_url);
    }

and then did a str_replace() for both strings. Might be overkill but it will work no matter what protocol $attachment->image_src_large[0] turns out to be.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748866298a314373.html

最新回复(0)