2016-10-11 34 views
0

我想检查是否有任何背后的/index.php/' 参数必须被忽略,并且没有/index.php/的baseurl也必须被忽略。 我知道PHP在链接中获得secundary的东西

if (strpos($url, 'contact') !== false){ } 

,但我不知道如何使用它来检查URL的第二部分,任何东西,但参数。 我该怎么做?

例如:

localhost:8080/ - returns false. 
localhost:8080/index.php - returns false. 
localhost:8080/index.php/ - returns false. 
localhost:8080/index.php?id=0 - returns false. 
localhost:8080/index.php/contact - returns true. 
+0

您可以使用基名+ parse_url或explose例如:回声基本名称(parse_url($ _ SERVER [ 'REQUEST_URI'],PHP_URL_PATH)); – Kate

+0

您在这里添加了Joomla标签。你想做什么?如果你添加一些上下文,你可能会得到更好的答案。 –

+0

joomla标签在这里,因为该网站是建立在joomla上。 这个结果只有一个页面可以处理,而不是每个链接都有一个单独的页面。 –

回答

0

您需要解析的字符串。

$url = "localhost:8080/index.php/contact?id=0"; 

// Split the string using delimiter "/index.php/" 
$url = explode("/index.php/", $url); 

// We have parts 
if (isset($url[1])) { 

    // We want the first part after "/index.php/" 
    // And we don't want anything after ? 
    $url = explode("?", $url[1])[0]; 

// We have no parts, change the array to an empty string 
} else { 

    $url = ""; 
} 
+0

获取url的所有部分, 我如何获得index.php后面的任何值 –

+0

我试过什么在你的编辑,但是, 我得到这个错误:未定义偏移量:1当我尝试加载本地主机:8080/index.php localhost:8080/index.php /很好。 localhost:8080/and localhost:8080也会给出错误 –

+0

是的,你需要检查字符串是否为空。 – BOOSTERBOMB

0

可以正则表达式的index.php后会发生什么

preg_match("/index\.php\/(.*?)^/ims","localhost:8080/index.php/contact",$matches); 
print_r($matches);