我将此字符串输入数据库:
$str = "<ol><li><span style="color:rgb(255,153,0);"><b style="color:rgb(255,153,0);">Maximum</b></span> authority</li><li>Innovative response</li><li>Freedom<br></li></ol>";
我想将字符串拆分为一个数组,将<li>
内的值作为值,并得出类似的结果:
[0] => <span style="color:rgb(255,153,0);"><b style="color:rgb(255,153,0);">Maximum</b></span> authority
[1] => Innovative response
[2] => Freedom<br>
但是,我只想出这个解决方案:
$xml = new DOMDocument();
$xml->loadHTML($str);
foreach($xml->getElementsByTagName('li') as $li)
$final_list[] = $li->nodeValue;
// Results
[0] => Maximum authority
[1] => Innovative response
[2] => Freedom
它删除了<li>
中的HTML标记,这不是我预期的结果。有什么想法可以改善这个吗?
答案 0 :(得分:0)
请参考http://www.php.net/manual/en/domdocument.savexml.php您需要使用saveXML来保留HTML
$final_list[] = htmlentities($xml->saveXML($li), ENT_QUOTES, 'UTF-8')
答案 1 :(得分:0)
检查此解决方案:
$xml = new DOMDocument();
$xml->loadHTML($str);
$final_list = array();
foreach($xml->getElementsByTagName('li') as $li) {
$tmp_dom = new DOMDocument();
$tmp_dom->appendChild($tmp_dom->importNode($li, true));
$h = trim($tmp_dom->saveHTML());
$final_list[] = substr($h, strpos($h,'>')+1, -(strlen($li->nodeName)+3));
}