2015-04-06 56 views
0

我想知道如果URL的模式/第一个字符串是特定的,我该怎么做。PHP - Preg match URL

目前,我得到当前网址是这样的:

<?php if($_SERVER['REQUEST_URI'] == "/profile"){echo "class='selected'";} ?> 

虽然,我想知道我怎么可以将上面的代码/ profile文件到URL包含“/ profile文件” - 例如:

/profile/??? 

谢谢!

回答

0

尝试'/^(\/profile)(\/|$)/'作为您的preg_match模式。

的源代码:

if (preg_match('/^(\/profile)(\/|$)/', $_SERVER['REQUEST_URI'])) { 
    ... 
} 
+0

这是更好,并回答了这个问题(如果我只是正确地读了它的话)。 – TimBrownlaw 2015-04-06 08:16:34

0

可以使用

如果(的preg_match( “@ /轮廓@”,$ _ SERVER [ 'REQUEST_URI'])> 0){回声 “类= '选择'”;}

我有使用'@'字符作为正则表达式的分隔符是不同的。

+1

它将为 “/仿形” 和 “/用户/配置文件” 回归也是如此。 – 2015-04-06 07:50:46

+0

是的,如果/ profile在URI中的任何地方,它会返回TRUE。 – TimBrownlaw 2015-04-06 07:55:09