我在wordpress应用程序中尝试用php进行简单的str\\u替换时遇到了一个非常奇怪的问题。
我正在尝试使用wordpress函数创建图像路径,以获取站点路径和资源路径。下面是我尝试过的3种方法,都给出了我不理解的结果。
原始方法:(这在我自己的localhost上运行,但在prod中,管理端为前端提供了不同的$fullpath)
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.\'/\', \'\', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;
前端的输出为$fullpath
string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
但在Admin End was中时(请注意双正斜杠)
string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
我注意到在生产服务器上
$attachment->image_src_large[0]
没有像在dev服务器上那样包含域。因此,考虑到这一点,为了解决管理端的双斜杠问题,我尝试了许多方法,详情如下-这是事情开始变得有点奇怪的时候:
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"
奇怪的是$finalpath字符串与$fullpath sting的大小,看起来str\\u replace可以工作,但当php读取字符串时,它会替换/使用域
4、尝试转义要插入的正斜杠
...
$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"
我搞不懂这个!如果有人能帮忙,我将不胜感激!