问题替换字符串中的正斜杠 (php)
Issue replacing forward slash in string (php)
我在 wordpress 应用程序中试图在 php 中执行一个简单的 str_replace 时遇到了一个非常奇怪的问题。
我正在尝试使用 wordpress 函数创建图像路径以获取站点路径和资源路径。以下是我尝试过的 3 种方法,都给出了我不理解的结果。
原始方法:(这在我自己的本地主机上有效,但在生产时,管理员端给前端端一个不同的 $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"
但是在管理端是(注意双正斜杠)
string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
我注意到在生产服务器上 $attachment->image_src_large[0]
没有像在开发服务器上那样包含域。因此,考虑到这一点,并试图解决管理员端的双斜线问题,我尝试了多种方法,详述如下 - 这是事情开始变得有点奇怪的时候:
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 字符串的大小,看起来 str_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"
我正在寻找的输出是这个 - string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
我搞不懂这个!如果有人能提供帮助,我将不胜感激!
因为你已经有了
$homepath = '/usr/www/users/currentuser/'
$filepath = /wp-content/uploads/2017/10/image1-2962-1024x684.jpg'
(来自$attachment->image_src_large[0]
)
你可以做到:
$fullpath = substr($homepath, 0, strlen($homepath)-1) . $filepath;
$fullpath = str_replace('https://currentdomain.co.za/', '', $fullpath);
$fullpath = str_replace('//', '/', $fullpath);
通过使用 substr
函数,移除 $homepath
中的最后一个 /
(此代码从字符串的开头读取到最后一个字符,即 /
).这可能是可选的,因为 browser/web 服务器应该自动删除额外的 /
.
接下来,将域名替换为空字符串,并将 //
替换为空字符串,具体顺序如下。
希望这能解决您的问题
我想出了解决办法!并不是说它解释了我看到的一些奇怪的 str_replace activity,所以如果有人能解释一下,请解释一下。
但要解决我遇到的最初问题 - 行
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
仅对 后端 或 前端有影响,但 对两者都没有影响 因为$attachment->image_src_large[0]
每个都不一样。在前端我会得到 https://...
但在后端,无论出于何种原因,我都会得到 http://...
.
所以我将 $site_url = get_site_url();
扩展为:
$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);
}
然后对两个字符串都做了 str_replace()
。可能有点矫枉过正,但无论 $attachment->image_src_large[0]
结果是什么协议,它都会起作用。
我在 wordpress 应用程序中试图在 php 中执行一个简单的 str_replace 时遇到了一个非常奇怪的问题。
我正在尝试使用 wordpress 函数创建图像路径以获取站点路径和资源路径。以下是我尝试过的 3 种方法,都给出了我不理解的结果。
原始方法:(这在我自己的本地主机上有效,但在生产时,管理员端给前端端一个不同的 $fullpath)
$site_url = get_site_url();
$homepath = get_home_path();
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
$fullpath = $homepath.$filepath;
前端的输出是
的 $fullpathstring(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
但是在管理端是(注意双正斜杠)
string(114) "/usr/www/users/currentuser//wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
我注意到在生产服务器上 $attachment->image_src_large[0]
没有像在开发服务器上那样包含域。因此,考虑到这一点,并试图解决管理员端的双斜线问题,我尝试了多种方法,详述如下 - 这是事情开始变得有点奇怪的时候:
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 字符串的大小,看起来 str_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"
我正在寻找的输出是这个 - string(114) "/usr/www/users/currentuser/wp-content/uploads/2017/10/image1-2962-1024x684.jpg"
我搞不懂这个!如果有人能提供帮助,我将不胜感激!
因为你已经有了
$homepath = '/usr/www/users/currentuser/'
$filepath = /wp-content/uploads/2017/10/image1-2962-1024x684.jpg'
(来自$attachment->image_src_large[0]
)
你可以做到:
$fullpath = substr($homepath, 0, strlen($homepath)-1) . $filepath;
$fullpath = str_replace('https://currentdomain.co.za/', '', $fullpath);
$fullpath = str_replace('//', '/', $fullpath);
通过使用 substr
函数,移除 $homepath
中的最后一个 /
(此代码从字符串的开头读取到最后一个字符,即 /
).这可能是可选的,因为 browser/web 服务器应该自动删除额外的 /
.
接下来,将域名替换为空字符串,并将 //
替换为空字符串,具体顺序如下。
希望这能解决您的问题
我想出了解决办法!并不是说它解释了我看到的一些奇怪的 str_replace activity,所以如果有人能解释一下,请解释一下。
但要解决我遇到的最初问题 - 行
$filepath = str_replace($site_url.'/', '', $attachment->image_src_large[0]);
仅对 后端 或 前端有影响,但 对两者都没有影响 因为$attachment->image_src_large[0]
每个都不一样。在前端我会得到 https://...
但在后端,无论出于何种原因,我都会得到 http://...
.
所以我将 $site_url = get_site_url();
扩展为:
$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);
}
然后对两个字符串都做了 str_replace()
。可能有点矫枉过正,但无论 $attachment->image_src_large[0]
结果是什么协议,它都会起作用。