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

时间:2016-09-20 16:03:54

标签: php model-view-controller

我正在使用模型视图控制器,在我的网址中,我需要在网址的末尾加斜杠。如何检查它是否有斜线?

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

3 个答案:

答案 0 :(得分:2)

例如:

if(strrev($url)[0]==='/') {
 //  the last character in the url is a slash
}

答案 1 :(得分:2)

您可以先获取整个网址然后获取最后一个字符

来实现此目的
$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

}

答案 2 :(得分:1)

你有很多可能性:

有斜线:

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

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