preg_match_all匹配多个字符串并获取用双引号写的值

时间:2016-09-02 06:54:54

标签: php preg-match-all

最近I asked a question关于preg_match_all与php,我得到了完美的解决方案,但现在我有一些不同的场景

这是我的字符串:

$html = '<p> This is my Home Page.</p><p><span style="line-height: 1.42857;">
        {{ type="slider" pagename="slider-1" }} 
        {{ type="banner"  pagename="banner-1" }} 
        {{ type="testimonial"  pagename="testimonial-1" }}
        </span></p><p> </p>';

我的代码:

preg_match_all('/{{ type="(.+?)" }}/', $html, $matches, PREG_SET_ORDER);

echo "<pre>";

print_r($matches);

foreach ($matches as  $val) {

    echo $val[1];
    echo "<br/>";
}
?>

我得到的结果:

Array
(
    [0] => Array
        (
            [0] => {{ type="slider" pagename="slider-1" }}
            [1] => slider" pagename="slider-1
        )

    [1] => Array
        (
            [0] => {{ type="banner"  pagename="banner-1" }}
            [1] => banner"  pagename="banner-1
        )

    [2] => Array
        (
            [0] => {{ type="testimonial"  pagename="testimonial-1" }}
            [1] => testimonial"  pagename="testimonial-1
        )

)
slider" pagename="slider-1
banner"  pagename="banner-1
testimonial"  pagename="testimonial-1

如何获取字符串中sliderpagename中写入的{{}}变量的值?

1 个答案:

答案 0 :(得分:2)

下面:

/{{ type=\"(.+)\"\s+pagename=\"(.+)\" }}/i

行动中:https://regex101.com/r/tX9vD3/1