您的位置:首页 > 编程语言 > PHP开发

php_cawler_html嵌套标签清洗

2015-08-13 18:29 751 查看
主要处理 嵌套 div,正则无法很好的处理清洗


比如文本: 想要移除 class =quizPutTag 的div  ,内部可能嵌套的还有未知层级的div【前提是html文本段是闭合标签的】


这是<div>test<div class="quizPutTag">test</div><div class="quizPutTag">H<sub>2</sub>C<sub>2</sub>O<sub>4</sub>•2H<sub>2</sub>O<span dealflag="1" class="MathJye" mathtag="math" style="whiteSpace:nowrap;wordSpacing:normal;wordWrap:normal"><table cellpadding="-1" cellspacing="-1" style="margin-right:1px"><tbody><tr><td style="border-bottom:1px solid black;padding-bottom:1px;font-size:90%"><table style="margin-right: 1px" cellspacing="-1" cellpadding="-1"><tbody><tr><td>  △  </td></tr><tr><td style="font-size: 90%"><div style="border-top:1px solid black;line-height:1px">.</div></td></tr></tbody></table></td></tr><tr><td> </td></tr></tbody></table></span>CO↑+CO<sub>2</sub>↑+3H<sub>2</sub>O↑</div>保留的</div>


实现code


<?php
include "./simple_html_dom.php";
$output="";
$input=fgets(fopen($argv[1],'r'));
echo $input."\n\n";
$html = new simple_html_dom();
$html->load('<html><body>'.$input.'</body></html>');
$divsq = $html->find('div.quizPutTag');
$divsa = $html->find('div.sanwser');
$output=str_replace($divsq,'___',$input);
$output=str_replace($divsa,'',$output);
echo $divs[0]."\n";
echo $html."\n";
$html->clear();
echo $output."\n";

?>


simple_html_dom.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: