2016-09-20 85 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 
}