Php preg_匹配所有已命名的子模式

Php preg_匹配所有已命名的子模式,php,preg-match-all,Php,Preg Match All,我想不出下面的表达式: preg_match_all('/[(?P<slug>\w+\-)\-(?P<flag>(m|t))\-(?P<id>\d+)]+/', $slugs, $matches); 您的表达式看起来像是试图将路径元素拆分为slug、flag和id部分。它失败了,因为括号[…]用于匹配字符,但在这里,它似乎用于将内容放在一起,比如括号。它也无法正确获取slug部分,因为它不允许超过一个系列的word\w和dash-字符。即,该部分与“arti

我想不出下面的表达式:

preg_match_all('/[(?P<slug>\w+\-)\-(?P<flag>(m|t))\-(?P<id>\d+)]+/', $slugs, $matches);

您的表达式看起来像是试图将路径元素拆分为slug、flag和id部分。它失败了,因为括号
[…]
用于匹配字符,但在这里,它似乎用于将内容放在一起,比如括号。它也无法正确获取slug部分,因为它不允许超过一个系列的word
\w
和dash
-
字符。即,该部分与“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);

你的问题是什么。你想做什么?给我们一个例子,说明你想在PHP手册的文档章节中做什么。每件事都在这一页上解释了。
$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
        )

)