$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Issue replacing forward slash in string|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Issue replacing forward slash in string

matteradmin9PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far