在 preg_match 中获取匹配项无效

Getting matches in preg_match not working

我一直在努力了解如何让 preg_match 使用以下模式:

\[flexvideo.* id="(?<id>[^"']+)" type="(?<type>[^"']+)".*\]\iU

实际上,我有以下代码:[flexvideo id="" type="" aspect=""]

也可以写成:[flexvideo id='' type='' aspect='']

属性可以按任何顺序排列。

我不明白的是,当我使用 ' 而不是 " 时,我没有得到匹配的结果,而另一个则没有。

此外,如果没有匹配项,我希望匹配项显示空字段。

这是我一直在使用的东西:http://www.phpliveregex.com/p/dGA

使用

  • ["\'] => " 或 '
  • [^"\']* => 除 " 和 ' 之外的任何字符 0 次或多次

代码:

$str = '[flexvideo id="" type=\'biuahsdiuasd\' aspect="c"]\iU';
$reg = '/\[flexvideo\s+id=["\'](?<id>[^"\']*)["\']\s+type=["\'](?<type>[^"\']*).*?\]/';
preg_match($reg, $str, $match);
print_r($match);

输出:

Array
(
    [0] => [flexvideo id="" type='biuahsdiuasd' aspect="c"]
    [id] => 
    [1] => 
    [type] => biuahsdiuasd
    [2] => biuahsdiuasd
)