php操作html元素,使用PHP Simple HTML DOM Parser

来源:互联网 发布:c语言多个else if 编辑:程序博客网 时间:2024/05/21 17:04

今天需要一个操作html元素的的功能,记得php有xml的,但是用的真心少,在网上搜下教程,没想到还有PHP Simple HTML DOM Parser这个开源项目,看了下介绍,可以像jquery那样操作html,赶紧下载下来体验下得意

代码如下

<?phpheader('Content-type:text/html;charset=gb2312;');//首先引入类文件require 'simple_html_dom.php';//读取文件里面的内容$str=file_get_contents('tbody.txt');//实例化对象$html=new simple_html_dom();//存放取出数据的数组$temp_arr=array();//解析html字符串$html->load($str);//循环取出的变量,因为是要取出tr下面的某几个td里面的内容,所以这里先查找出所有trforeach ($html->find('tr') as $key => $value) {$temp=array();//第一行是边个的标题,跳过if ($key==0) {continue;}//循环tr下面的tdforeach ($value->children() as $key_1 => $value_1) {//因为这个表格是不规则的,用到了rowspan,所以这里这样操作了下if (isset($value_1->attr['class'])&&$value_1->attr['class']=='xxx') {//find是找出当前元素下面的标签,后面没数字的话应该就是返回所有的,有数字的话就是返回指定的那一个$temp[]=trim($value_1->find('a',0)->plaintext);$temp[]=trim($value->children($key_1+3)->plaintext);$temp[]=trim($value->children($key_1+4)->plaintext);$temp[]=trim($value->children($key_1+5)->plaintext);$temp_arr[]=$temp;}continue;}}var_dump($temp_arr);?>


中文文档网址:http://www.ecartchina.com/php-simple-html-dom/manual.htm

0 0