[英]PHP - Get URL and remove the trailing slash
So I've created this function that get's the URL of the Website, but I also want it to remove the trailing /
as well. 因此,我创建了此功能,该功能获取网站的URL,但我也希望它也删除结尾的
/
。
So far I have this: 到目前为止,我有这个:
function base_url() {
$base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
return $base_url.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
But I don't know how to remove the trailing /
from the URL. 但是我不知道如何从URL中删除尾随
/
。
I've also tried to add the rtrim
but still no luck: 我也尝试添加
rtrim
但仍然没有运气:
function base_url() {
$base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$base_url = rtrim($base_url, '/');
return $base_url.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
}
Image of the URL - 网址图片-
Try it like this: 像这样尝试:
function base_url() {
$base_url = $_SERVER['HTTPS'] == 'on' ? 'https' : 'http';
$base_url.= '://'.$_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
return rtrim( $base_url, '/' );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.