这是一些html代码,如何使用simplehtmldom解析html并保存为json数据?
<p>text1</p>
<div>
<p>text2</p>
<div>
<ul>
<li>subtext1</li>
<li>subtext2</li>
</ul>
<p>text3</p>
<div>
<div>
<p>text4</p>
</div>
</div>
<ul>
<li>subtext1</li>
<li>subtext2</li>
</ul>我需要解析原始订单的<ul> <li> <p>节点,然后保存到json数据。
[
{
"p":"text1"
},
{
"p":"text2"
},
{
"ul":[
{
"li":"subtext1"
},
{
"li":"subtext2"
}
]
},
{
"p":"text3"
},
{
"p":"text4"
},
{
"ul":[
{
"li":"subtext3"
},
{
"li":"subtext4"
}
]
}
]发布于 2013-02-21 19:35:34
include('simple_html_dom.php');
$html = str_get_html(YourContentHere);
$data = array();
$count = 0;
foreach($html->find('p') as $li)
{
$data[$count]['p'] = $li->innertext;
$count++;
}
foreach($html->find('ul') as $ul)
{
foreach($ul->find('li') as $li )
$data[$count]['ul'][]['li'] = $li->innertext;
$count++;
}
echo json_encode($data);试试这个,也许我弄错了
发布于 2013-02-21 19:31:22
json_encode((array) simplexml_load_string($html_input)发布于 2013-02-21 19:46:49
我自己解决,当然我会完成json_encode()部分。获取我需要的json树。谢谢。
foreach($html->find('p, ul') as $foreach){
if($foreach->tag =='p'){
$out .= json_encode($foreach->plaintext);
}else{
foreach($foreach->find('li') as $li){
$out .= json_encode($li->plaintext);
}
}
}https://stackoverflow.com/questions/15001066
复制相似问题