2016-09-20 60 views
0

我正在使用模型视图控制器,并且在我的URL中,我需要在URL的末尾有斜杠。如何检查它是否有斜线?PHP:检查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 
+0

你的'rtrim'不正确。不要连接结尾字符是第二个参数。然后你可以追加一个'/'因为你知道末尾的斜线被删除了。 – chris85

回答

1

您可以首先得到整个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 

} 
+0

它的工作表示感谢 –

1

你有很多可能性,它:

有斜线:

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

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

比如像这样:

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