我有一个字符串,HTML属性:PHP - 拆分HTML的字符串属性为索引数组
$attribs = ' id= "header " class = "foo bar" style ="background-color:#fff; color: red; "';
如何该字符串转换成一个索引数组,如:
array(
'id' => 'header',
'class' => array('foo', 'bar'),
'style' => array(
'background-color' => '#fff',
'color' => 'red'
)
)
,所以我可以使用PHP array_merge_recursive函数来合并2组HTML属性。
谢谢
谢谢浓汤,您正则表达式很酷。 唯一的问题是$ attrs ['class']或$ attrs ['style']正在返回字符串:所以很难将它们与另一个$ attribs字符串合并,例如合并那两组属性: $ attribs1 ='class =“foo bar”'; $ attribs2 ='class =“lorem”'; 你有什么想法可以增强它? – abernier 2009-07-05 10:36:34
我真的很喜欢这个解决方案...但我没有得到正则表达式xD它对我的头来说有一点点 – lumio 2012-07-10 20:46:37
我刚刚写了一个替代正则表达式,它也解析了HTML5样式布尔属性(没有=符号)和使用引用的引用:`(\ w +)\ s *(= \ s *(['''))(。*?)\ 2 \ s)' – 2012-11-11 22:45:14