如何从preg_match_all()访问匹配?

时间:2016-06-08 21:40:21

标签: php regex preg-match-all

我试图了解preg_match_all()的方式,但我并不理解。这段代码有什么问题?我想用它获取URL:

<?php
$content = '<a href="http://www.google.com">Google</a> Lorem ipsum dolor sit amet, consectetur adipiscing elit <a href="http://stackoverflow.com">Stackoverflow</a>';
$search = preg_match_all("/<a\s[^>]*href=\"([^\"]*)\"[^>]*>(.*)<\/a>/siUU", $content, $matches);

foreach ($matches as $match){
    echo $match;
}
?>

3 个答案:

答案 0 :(得分:2)

作为个人观点,我绝对更喜欢标记PREG_SET_ORDER(默认为PREG_PATTERN_ORDER)。

$search = preg_match_all("/<a\s[^>]*href=\"([^\"]*)\"[^>]*>(.*)<\/a>/siU",
          $content, $matches, PREG_SET_ORDER);

AbraCadaver概述的例子将如下安排:

Array
(
    [0] => Array
        (
            [0] => <a href="http://www.google.com">Google</a>
            [1] => http://www.google.com
            [2] => Google

        )

    [1] => Array
        (
            [0] => <a href="http://stackoverflow.com">Stackoverflow</a>
            [1] => http://stackoverflow.com
            [2] => Stackoverflow
        )

)

2个结果,每个3个(子)匹配 - 这更容易使用。

这样的东西
foreach ($matches AS $match){
  echo $match[0]; // HTML
  echo $match[1]; // URL
  echo $match[2]; // PageName
}

答案 1 :(得分:1)

你的模式有效,尽管你指出你有两个U修饰符,但是print_r($matches);产生了这个:

Array
(
    [0] => Array
        (
            [0] => <a href="http://www.google.com">Google</a>
            [1] => <a href="http://stackoverflow.com">Stackoverflow</a>
        )

    [1] => Array
        (
            [0] => http://www.google.com
            [1] => http://stackoverflow.com
        )

    [2] => Array
        (
            [0] => Google
            [1] => Stackoverflow
        )
)

所以你想循环$matches[1],它对应于应该获得URL的第一个捕获组([^\"]*)

foreach ($matches[1] as $match){
    echo $match;
}

$matches[0]是完整的模式匹配,$matches[1]是第一个捕获组()$matches[2]是第二个捕获组()等... < / p>

答案 2 :(得分:0)

您只需要PREG_SET_ORDER。如果您使用命名捕获,它会更清楚:

$mystring="abc";
preg_match_all('/(?<letter>[a-z])/', $mystring, $matches);
print($matches["letter"][1]); // "b"

$mystring="abc";
preg_match_all('/(?<letter>[a-z])/', $mystring, $matches, PREG_SET_ORDER);
print($matches[1]["letter"]); // "b"