Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.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验证url以路径的两部分结尾,一个是固定的,一个是变量_Php_Pattern Matching_Preg Match - Fatal编程技术网

PHP验证url以路径的两部分结尾,一个是固定的,一个是变量

PHP验证url以路径的两部分结尾,一个是固定的,一个是变量,php,pattern-matching,preg-match,Php,Pattern Matching,Preg Match,我的网站上有如下URL: http://example.com/item/one/ http://example.com/item/two/ http://example.com/item/one/something http://example.com/item/two/someotherthing http://example.com/other/one/ http://example.com/other/two/ 我想检查url并重定向它是否匹配/item/one/或/item/two/

我的网站上有如下URL:

http://example.com/item/one/
http://example.com/item/two/
http://example.com/item/one/something
http://example.com/item/two/someotherthing
http://example.com/other/one/
http://example.com/other/two/
我想检查url并重定向它是否匹配
/item/one/
/item/two/
,但不检查它是否只匹配
/one/
/two/
,也不匹配任何像
某物那样更深的字符串

理想情况下,我希望匹配同时包含
/item/
和之后的一个最终路径的任何内容(即
/item/three/
/item/three/

完成比赛的最佳方式是什么?preg_match(不确定如何为此编写)?爆炸

更新 尝试了以下操作:

$thisurl = $_SERVER['REQUEST_URI'];
if (preg_match("/^\/item\/(.*)\/$/", $thisurl)) {
    echo "it matches";
} else {
    echo "nope nope nope";
}
这在我的代码中有效,但在我的代码中失败(因为它也适用于以下情况:

http://example.com/item/one/something
http://example.com/item/two/someotherthing

它不应该这样做。

这就是如何使用explode函数进行操作

 $url = [
        'http://example.com/item/one/',
        'http://example.com/item/two/',
        'http://example.com/item/one/something',
        'http://example.com/item/two/someotherthing',
        'http://example.com/other/one/',
        'http://example.com/other/two/'
    ];

    foreach($url as $row){
        $path = explode('/',str_replace('http://example.com/','',rtrim($row,'/')));

        if(count($path) != 2){
            continue;
        }

        if($path[0] != "item"){
            continue;
        }

        if(in_array($path[1],[
            'one',
            'two'
        ])){
            echo 'redirect...';
        }
    }

不过,更好的答案可能需要使用某种形式的正则表达式。

最终使用了以下内容:

if (preg_match("/^\/item\/[-a-z]+\/$/", $thisurl)) { //matches any lowercase word that may or may not contain a dash
echo "it matches";
} else {
echo "nope nope nope";
}

尽管告诉我,
preg\u match(“/^\/item\/(.*)\/$/”,$thisurl)
会起作用,但它没有起作用。

这个正则表达式起作用:

~/item/[^/]+/$~i
说明:

~           : regex delimiter
  /item/    : literally
  [^/]+     : 1 or more character that is NOT a slash
  /         : a slash
  $         : end of string
~i          : regex delimiter + case insensitive
$tests = array(
    'http://example.com/item/one/',
    'http://example.com/item/two/',
    'http://example.com/item/one/something',
    'http://example.com/item/two/someotherthing',
    'http://example.com/other/one/',
    'http://example.com/other/two/'
);
foreach($tests as $test) {
    echo "$test --> ";
    if (preg_match('~/item/[^/]+/$~i', $test)) {
        echo "valid\n";
    } else {
        echo "invalid\n";
    }
}
http://example.com/item/one/ --> valid
http://example.com/item/two/ --> valid
http://example.com/item/one/something --> invalid
http://example.com/item/two/someotherthing --> invalid
http://example.com/other/one/ --> invalid
http://example.com/other/two/ --> invalid
正在使用中:

~           : regex delimiter
  /item/    : literally
  [^/]+     : 1 or more character that is NOT a slash
  /         : a slash
  $         : end of string
~i          : regex delimiter + case insensitive
$tests = array(
    'http://example.com/item/one/',
    'http://example.com/item/two/',
    'http://example.com/item/one/something',
    'http://example.com/item/two/someotherthing',
    'http://example.com/other/one/',
    'http://example.com/other/two/'
);
foreach($tests as $test) {
    echo "$test --> ";
    if (preg_match('~/item/[^/]+/$~i', $test)) {
        echo "valid\n";
    } else {
        echo "invalid\n";
    }
}
http://example.com/item/one/ --> valid
http://example.com/item/two/ --> valid
http://example.com/item/one/something --> invalid
http://example.com/item/two/someotherthing --> invalid
http://example.com/other/one/ --> invalid
http://example.com/other/two/ --> invalid
输出:

~           : regex delimiter
  /item/    : literally
  [^/]+     : 1 or more character that is NOT a slash
  /         : a slash
  $         : end of string
~i          : regex delimiter + case insensitive
$tests = array(
    'http://example.com/item/one/',
    'http://example.com/item/two/',
    'http://example.com/item/one/something',
    'http://example.com/item/two/someotherthing',
    'http://example.com/other/one/',
    'http://example.com/other/two/'
);
foreach($tests as $test) {
    echo "$test --> ";
    if (preg_match('~/item/[^/]+/$~i', $test)) {
        echo "valid\n";
    } else {
        echo "invalid\n";
    }
}
http://example.com/item/one/ --> valid
http://example.com/item/two/ --> valid
http://example.com/item/one/something --> invalid
http://example.com/item/two/someotherthing --> invalid
http://example.com/other/one/ --> invalid
http://example.com/other/two/ --> invalid