使用正则表达式获取所有属性

时间:2014-09-29 08:53:37

标签: php regex

我的输入字符串是

$center="[{video('whats-new/reaction-vid1.jpg')} width=\"580\" height=\"326\" alt=\"\" video=\"Zb36h4K2IKQ\"]";
$pattern="/\[{video\('([a-zA-Z0-9\/\-\_]+)'\)}\s+(width|height|alt|video)=\"[^\"]+\"\]/";
if(preg_match($pattern,$center,$matches)){
   print_r($matches);exit;
}

然而它不起作用。基本上我想要宽度,高度,alt和视频的额外属性。我已经尝试了半个小时。谁能指出我正确的方向?

1 个答案:

答案 0 :(得分:0)

使用preg_match_all代替preg_match

<?php
$center="[{video('whats-new/reaction-vid1.jpg')} width=\"580\" height=\"326\" alt=\"\" video=\"Zb36h4K2IKQ\"]";
$pattern="~video\('\K[^']*|(?:width|height|alt|video)=\"\K[^\"]*~";
preg_match_all($pattern, $center, $matches);
print_r($matches);
?>

输出:

Array
(
    [0] => Array
        (
            [0] => whats-new/reaction-vid1.jpg
            [1] => 580
            [2] => 326
            [3] => 
            [4] => Zb36h4K2IKQ
        )

)