PHP:检查URL末尾是否包含斜杠

3

我正在使用模型视图控制器,我的URL需要在结尾处有斜杠。如何检查是否有斜杠?

http://localhost/tabulation/event/event_name/<--I need to check this slash

$url = isset($_GET['url']) ? $_GET['url'] : null;
$url = rtrim($url. '/');
$url = explode('/', $url);
//if($url[2] has no backslashes then execute the condition

你的 rtrim 函数是错误的。不要将尾随的字符作为第二个参数连接起来。你可以直接添加一个 /,因为你知道尾随的斜杠已经被移除了。 - chris85
3个回答

12
您可以先获取整个URL,然后获取最后一个字符来实现这一点。
$getWholeUrl = "http://".$_SERVER['HTTP_HOST']."".$_SERVER['REQUEST_URI']."";

//The output would be 
/*
  http://localhost/tabulation/event/event_name/
*/

if(substr($getWholeUrl , -1)=='/'){
    //Add your condition here

}

4
例如这样:
if(strrev($url)[0]==='/') {
 //  the last character in the url is a slash
}

1
您有许多可能性:

有斜杠:

$url{strlen($url)-1} == '/'; (same as $url[strlen($url)-1] == '/';)

strrpos($a,'/') === strlen($a) -1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接