这是我的字符串。
$list = '
<div id="list">
<div class="item">foo: bar</div>
<div class="item">name: value</div>
<div class="item">color: red</div>
<div class="item">count: 1</div>
</div>
';
如何从这个html获取数据并添加到PHP数组的最佳方法? 我想收到:
$items = array('foo' => 'bar', 'name' => 'value', 'color' => 'red', 'count' => 1);
答案 0 :(得分:2)
使用DOMDocument和DOMXpath解析html并获取内容
然后,您可以在$str = <<<EOF
<div id="list">
<div class="item">foo: bar</div>
<div class="item">name: value</div>
<div class="item">color: red</div>
<div class="item">count: 1</div>
</div>
EOF;
//Parse the html data
$dom = new DOMDocument;
$dom->loadHTML($str);
$xpath = new DOMXpath($dom);
//Get only those divs which have class=item
$div_list = $xpath->query('//div[@class="item"]');
$content_arr = [];
foreach($div_list as $d){
$c = explode(": ", $d->nodeValue);
$content_arr[$c[0]] = $c[1];
}
var_dump($content_arr);
上拆分它们并将它们添加到数组中
像这样的东西 -
array(4) {
'foo' =>
string(3) "bar"
'name' =>
string(5) "value"
'color' =>
string(3) "red"
'count' =>
string(1) "1"
}
此输出 -
{{1}}
答案 1 :(得分:0)
var arr = [];
$('.item').each(function(){
var asdf = $(this).text();
var qwerty = asdf.split(":");
arr.push(qwerty['0'] + ' =>' + qwerty['1']);
});
alert(arr);
希望这可以帮助你.. =)
答案 2 :(得分:0)
您可以使用SimpleXML执行此操作。为此你需要像这样编码:
<?php
$html='<div id="list">
<div class="item">foo: bar</div>
<div class="item">name: value</div>
<div class="item">color: red</div>
<div class="item">count: 1</div>
</div>';
$xml = new SimpleXMLElement($html);
$result = $xml->xpath('//div[@id="list"]');
$items = array();
foreach($result AS $arrKeys => $arrValue){
foreach($arrValue AS $innerValue){
list($key,$value) = explode(":",$innerValue);
if(!empty($value)){
$items[$key] = $value;
}
}
}
print_r($items);
?>
这是您想要的一步一步的代码。