下面是一些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>
nod,然后保存到json数据中。
[
{
"p":"text1"
},
{
"p":"text2"
},
{
"ul":[
{
"li":"subtext1"
},
{
"li":"subtext2"
}
]
},
{
"p":"text3"
},
{
"p":"text4"
},
{
"ul":[
{
"li":"subtext3"
},
{
"li":"subtext4"
}
]
}
]
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);
试试这个,也许我有错误
json_encode((array) simplexml_load_string($html_input)
自己解决,当然我会完成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);
}
}
}