preg_match_all 具有命名的子模式
preg_match_all with named subpatterns
我无法理解以下表达式:
preg_match_all('/[(?P<slug>\w+\-)\-(?P<flag>(m|t))\-(?P<id>\d+)]+/', $slugs, $matches);
我的 $slugs 变量是这样的:
article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/
您的表达式看起来像是试图将路径元素拆分为 slug、flag 和 id 部分。它失败了,因为括号 [ ... ]
用于匹配字符,但在这里它似乎用于将事物放在一起,如括号。它也无法使 slug 部分正确,因为它不允许超过一系列的单词 \w
和破折号 -
字符。 IE。该部分匹配 'article-' 但不匹配 'article-slug-one-'.
也许这就是你想要的?
$slugs = 'article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/';
preg_match_all('/(?P<slug>[\w-]+)\-(?P<flag>[mt])\-(?P<id>\d+)/', $slugs, $matches);
echo "First slug : " . $matches['slug'][0], PHP_EOL;
echo "Second flag: " . $matches['flag'][1], PHP_EOL;
echo "Third ID : " . $matches['id'][2], PHP_EOL;
print_r($matches);
输出:
First slug : article-slug-one
Second flag: t
Third ID : 111581
Array
(
[0] => Array
(
[0] => article-slug-one-m-111617
[1] => article-slug-two-t-111611
[2] => article-slug-three-t-111581
[3] => article-slug-four-m-111609
)
[slug] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[1] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[flag] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[2] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[id] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
[3] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
)
我无法理解以下表达式:
preg_match_all('/[(?P<slug>\w+\-)\-(?P<flag>(m|t))\-(?P<id>\d+)]+/', $slugs, $matches);
我的 $slugs 变量是这样的:
article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/
您的表达式看起来像是试图将路径元素拆分为 slug、flag 和 id 部分。它失败了,因为括号 [ ... ]
用于匹配字符,但在这里它似乎用于将事物放在一起,如括号。它也无法使 slug 部分正确,因为它不允许超过一系列的单词 \w
和破折号 -
字符。 IE。该部分匹配 'article-' 但不匹配 'article-slug-one-'.
也许这就是你想要的?
$slugs = 'article-slug-one-m-111617/article-slug-two-t-111611/article-slug-three-t-111581/article-slug-four-m-111609/';
preg_match_all('/(?P<slug>[\w-]+)\-(?P<flag>[mt])\-(?P<id>\d+)/', $slugs, $matches);
echo "First slug : " . $matches['slug'][0], PHP_EOL;
echo "Second flag: " . $matches['flag'][1], PHP_EOL;
echo "Third ID : " . $matches['id'][2], PHP_EOL;
print_r($matches);
输出:
First slug : article-slug-one
Second flag: t
Third ID : 111581
Array
(
[0] => Array
(
[0] => article-slug-one-m-111617
[1] => article-slug-two-t-111611
[2] => article-slug-three-t-111581
[3] => article-slug-four-m-111609
)
[slug] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[1] => Array
(
[0] => article-slug-one
[1] => article-slug-two
[2] => article-slug-three
[3] => article-slug-four
)
[flag] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[2] => Array
(
[0] => m
[1] => t
[2] => t
[3] => m
)
[id] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
[3] => Array
(
[0] => 111617
[1] => 111611
[2] => 111581
[3] => 111609
)
)