Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-Preg匹配URL_Php_Preg Match - Fatal编程技术网

PHP-Preg匹配URL

PHP-Preg匹配URL,php,preg-match,Php,Preg Match,如果URL的模式/第一个字符串是特定的,我想知道如何做一些事情 目前,我得到的当前url如下: <?php if($_SERVER['REQUEST_URI'] == "/profile"){echo "class='selected'";} ?> 谢谢 您可以使用 如果(preg_match(@/profile@),$_SERVER['REQUEST_URI'])>0{echo“class='selected'”;} 我使用了“@”字符作为分隔符,使正则表达式不同。尝试将“/^(

如果URL的模式/第一个字符串是特定的,我想知道如何做一些事情

目前,我得到的当前url如下:

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

您可以使用

如果(preg_match(@/profile@),$_SERVER['REQUEST_URI'])>0{echo“class='selected'”;}

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

尝试将“/^(\/profile)(\/|$)/”作为预匹配模式。

源代码:

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

对于“/profiler”和“/user/profile”,它也将返回true。是的,如果/profile在URI中的任何位置,它将返回true。这会更好,并且回答了这个问题(如果我一开始就正确阅读它的话)。
if (preg_match('/^(\/profile)(\/|$)/', $_SERVER['REQUEST_URI'])) {
  ...
}