preg_match_all

(PHP 4, PHP 5, PHP 7, PHP 8)

preg_match_all執(zhí)行一個全局正則表達式匹配

說明

preg_match_all(
    string $pattern,
    string $subject,
    array &$matches = null,
    int $flags = 0,
    int $offset = 0
): int|false|null

搜索subject中所有匹配pattern給定正則表達式 的匹配結(jié)果并且將它們以flag指定順序輸出到matches中.

在第一個匹配找到后, 子序列繼續(xù)從最后一次匹配位置搜索.

參數(shù)

pattern

要搜索的模式,字符串形式。

subject

輸入字符串。

matches

多維數(shù)組,作為輸出參數(shù)輸出所有匹配結(jié)果, 數(shù)組排序通過flags指定。

flags

可以結(jié)合下面標記使用(注意不能同時使用PREG_PATTERN_ORDERPREG_SET_ORDER):

PREG_PATTERN_ORDER

結(jié)果排序為$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一個子組的所有匹配,以此類推。

<?php
preg_match_all
("|<[^>]+>(.*)</[^>]+>|U",
    
"<b>example: </b><div align=left>this is a test</div>",
    
$outPREG_PATTERN_ORDER);
echo 
$out[0][0] . ", " $out[0][1] . "\n";
echo 
$out[1][0] . ", " $out[1][1] . "\n";
?>

以上例程會輸出:

<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test

因此, $out[0]是包含匹配完整模式的字符串的數(shù)組, $out[1]是包含閉合標簽內(nèi)的字符串的數(shù)組。

如果正則表達式包含了帶名稱的子組,$matches 額外包含了帶名稱子組的鍵。

如果正則表達式里,子組名稱重名了,則僅最右側(cè)的子組儲存在 $matches[NAME] 中。

<?php
preg_match_all
(
    
'/(?J)(?<match>foo)|(?<match>bar)/',
    
'foo bar',
    
$matches,
    
PREG_PATTERN_ORDER
);
print_r($matches['match']);
?>

以上例程會輸出:

Array
(
    [0] => 
    [1] => bar
)

PREG_SET_ORDER

結(jié)果排序為$matches[0]包含第一次匹配得到的所有匹配(包含子組), $matches[1]是包含第二次匹配到的所有匹配(包含子組)的數(shù)組,以此類推。

<?php
preg_match_all
("|<[^>]+>(.*)</[^>]+>|U",
    
"<b>example: </b><div align=\"left\">this is a test</div>",
    
$outPREG_SET_ORDER);
echo 
$out[0][0] . ", " $out[0][1] . "\n";
echo 
$out[1][0] . ", " $out[1][1] . "\n";
?>

以上例程會輸出:

<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test

PREG_OFFSET_CAPTURE

如果這個標記被傳遞,每個發(fā)現(xiàn)的匹配返回時會增加它相對目標字符串的字節(jié)偏移量。 注意這會改變matches中的每一個匹配結(jié)果字符串元素,使其 成為一個第0個元素為匹配結(jié)果字符串,第1個元素為 匹配結(jié)果字符串在subject中的偏移量。

<?php
preg_match_all
('/(foo)(bar)(baz)/''foobarbaz'$matchesPREG_OFFSET_CAPTURE);
print_r($matches);
?>

以上例程會輸出:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => foobarbaz
                    [1] => 0
                )

        )

    [1] => Array
        (
            [0] => Array
                (
                    [0] => foo
                    [1] => 0
                )

        )

    [2] => Array
        (
            [0] => Array
                (
                    [0] => bar
                    [1] => 3
                )

        )

    [3] => Array
        (
            [0] => Array
                (
                    [0] => baz
                    [1] => 6
                )

        )

)

PREG_UNMATCHED_AS_NULL

傳入此標記,未匹配的子組報告為 null;否則會是空 string

如果沒有給定排序標記,假定設(shè)置為PREG_PATTERN_ORDER。

offset

通常, 查找時從目標字符串的開始位置開始??蛇x參數(shù)offset用于 從目標字符串中指定位置開始搜索(單位是字節(jié))。

注意:

使用 offset 參數(shù)不同于傳遞 substr($subject, $offset) 的 結(jié)果到 preg_match_all() 作為目標字符串,因為 pattern 可以包含斷言比如^, $ 或者 (?<=x) 。 示例查看 preg_match()。

返回值

返回完整匹配次數(shù)(可能是0),或者如果發(fā)生錯誤返回false

更新日志

版本 說明
7.2.0 現(xiàn)在 $flags 參數(shù)可以支持 PREG_UNMATCHED_AS_NULL。

范例

示例 #1 查找所有文本中的電話號碼。

<?php
preg_match_all
("/\(?  (\d{3})?  \)?  (?(1)  [\-\s] ) \d{3}-\d{4}/x",
                
"Call 555-1212 or 1-800-555-1212"$phones);
?>

示例 #2 查找匹配的HTML標簽(貪婪)

<?php
//\\2是一個后向引用的示例. 這會告訴pcre它必須匹配正則表達式中第二個圓括號(這里是([\w]+))
//匹配到的結(jié)果. 這里使用兩個反斜線是因為這里使用了雙引號.
$html "<b>bold text</b><a href=howdy.html>click me</a>";

preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/"$html$matchesPREG_SET_ORDER);

foreach (
$matches as $val) {
    echo 
"matched: " $val[0] . "\n";
    echo 
"part 1: " $val[1] . "\n";
    echo 
"part 2: " $val[2] . "\n";
    echo 
"part 3: " $val[3] . "\n";
    echo 
"part 4: " $val[4] . "\n\n";
}
?>

以上例程會輸出:

matched: <b>bold text</b>
part 1: <b>
part 2: b
part 3: bold text
part 4: </b>

matched: <a href=howdy.html>click me</a>
part 1: <a href=howdy.html>
part 2: a
part 3: click me
part 4: </a>

示例 #3 使用子命名組

<?php

$str 
= <<<FOO
a: 1
b: 2
c: 3
FOO;

preg_match_all('/(?P<name>\w+): (?P<digit>\d+)/'$str$matches);

/* 選擇方式 */
// preg_match_all('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);

print_r($matches);

?>

以上例程會輸出:

Array
(
    [0] => Array
        (
            [0] => a: 1
            [1] => b: 2
            [2] => c: 3
        )

    [name] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [digit] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [2] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

)

參見