PHP - 将一串HTML属性分成一个索引数组 [英] PHP - split a string of HTML attributes into an indexed array
本文介绍了PHP - 将一串HTML属性分成一个索引数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML属性的字符串:
$ attribs ='id =headerclass =foo barstyle =background-color:#fff; color:red;';
如何将该字符串转换为索引数组,如:
array(
'id'=>'header',
'class'=> array('foo','bar' ),
'style'=>数组(
'background-color'=>'#fff',
'color'=>'red'
)
)
所以我可以使用PHP array_merge_recursive函数来合并2组HTML属性。 / p>
谢谢
解决方案
使用SimpleXML:
<?php
$ attribs ='id =headerclass =foo barstyle =background-color:#fff ; 红色; ';
$ x = new SimpleXMLElement(< element $ attribs />);
print_r($ x);
?>
这假定属性总是名称/值对...
I've got a string with HTML attributes:
$attribs = ' id= "header " class = "foo bar" style ="background-color:#fff; color: red; "';
How to transform that string into an indexed array, like:
array(
'id' => 'header',
'class' => array('foo', 'bar'),
'style' => array(
'background-color' => '#fff',
'color' => 'red'
)
)
so I can use the PHP array_merge_recursive function to merge 2 sets of HTML attributes.
Thank you
解决方案
Use SimpleXML:
<?php
$attribs = ' id= "header " class = "foo bar" style ="background-color:#fff; color: red; "';
$x = new SimpleXMLElement("<element $attribs />");
print_r($x);
?>
This assumes that the attributes are always name/value pairs...
这篇关于PHP - 将一串HTML属性分成一个索引数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文