PHP - 预赛 URL

PHP - Preg match URL

如果 URL 的 pattern/first 字符串是特定的东西,我想知道我该怎么做。

目前,我得到 current url 这样的:

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

不过,我想知道如何将上述代码应用到 /profile 到包含“/profile”的 URLs - 例如:

/profile/???

谢谢!

你可以使用

if(preg_match("@/profile@",$_SERVER['REQUEST_URI']) > 0){echo "class='selected'";}

我使用“@”字符作为正则表达式不同的分隔符。

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

源代码:

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