我想知道如果URL的模式/第一個字符串是特定的,我該怎麼做。PHP - Preg match URL
目前,我得到當前網址是這樣的:
<?php if($_SERVER['REQUEST_URI'] == "/profile"){echo "class='selected'";} ?>
雖然,我想知道我怎麼可以將上面的代碼/ profile文件和到URL包含「/ profile文件」 - 例如:
/profile/???
謝謝!
我想知道如果URL的模式/第一個字符串是特定的,我該怎麼做。PHP - Preg match URL
目前,我得到當前網址是這樣的:
<?php if($_SERVER['REQUEST_URI'] == "/profile"){echo "class='selected'";} ?>
雖然,我想知道我怎麼可以將上面的代碼/ profile文件和到URL包含「/ profile文件」 - 例如:
/profile/???
謝謝!
嘗試'/^(\/profile)(\/|$)/'
作爲您的preg_match模式。
的源代碼:
if (preg_match('/^(\/profile)(\/|$)/', $_SERVER['REQUEST_URI'])) {
...
}
可以使用
如果(的preg_match( 「@ /輪廓@」,$ _ SERVER [ 'REQUEST_URI'])> 0){回聲 「類= '選擇'」;}
我有使用'@'字符作爲正則表達式的分隔符是不同的。
它將爲 「/仿形」 和 「/用戶/配置文件」 迴歸也是如此。 – 2015-04-06 07:50:46
是的,如果/ profile在URI中的任何地方,它會返回TRUE。 – TimBrownlaw 2015-04-06 07:55:09
這是更好,並回答了這個問題(如果我只是正確地讀了它的話)。 – TimBrownlaw 2015-04-06 08:16:34