php替换文本中的多个占位符返回1个元素而不是2

时间:2018-02-01 09:43:18

标签: php preg-match

我有一个字符串,我想在一个数组中返回2个占位符,我该如何实现呢?

$text = 'is simply dummy text of the printing and [[file::aaa]] typesetting industry. Lorem Ipsum has been the [[file::bbb]] standard dummy text ever since the 1500s.';

if (preg_match("/\[\[.*\]\]/", $text, $matches)) {
    print_r($matches);
}

1 个答案:

答案 0 :(得分:1)

你可以尝试这样的事情:

if (preg_match_all("/\[\[(.*?)\]\]/", $text, $matches)) {
    print_r($matches);
}

输出:

Array
(
    [0] => Array
        (
            [0] => [[file::aaa]]
            [1] => [[file::bbb]]
        )

    [1] => Array
        (
            [0] => file::aaa
            [1] => file::bbb
        )

)