修改simple_html_dom源码 实现删除指定html标签

simple_html_dom是php解析html的简便而强大的工具。simple_html_dom.php源码文件可以在网上搜到,然后include进来就可以调用了。

但是simple_html_dom中没有提供删除指定标签的功能,所以下面实现以下,只要在simple_html_dom_node类中加上如下代码:

/*add by wangyu 2013-08-19
       *description:remove node with given tag
       *index begin with 0
       */
      function find_and_remove(&$p, $tag, &$index)
      {
          static $is_find = 0;
          if($is_find == 1)
          {
              return;
          }
          $idx = 0;
          foreach($p->nodes as $n)
          {
              if($n->tag===$tag && $index===0)
              {
                  unset($p->nodes[$idx]);     //remove the start tag, such as <img src="">
                  if(preg_match("<[\s ]*/[\s ]*img[\s ]*>", $p->nodes[$idx+1]->outertext))
                  {
                      unset($p->nodes[$idx+1]);   //remove the end tag, such as </img>
                  }
                  $is_find = 1;
                  return;
              }
              if($n->tag === $tag)
	      {
                  $index--;
              }
              if($n->has_child())
              {
                  $this->find_and_remove($n, $tag, $index);
              }
              $idx++;
          }
      }      
      function remove_node($tag, $index)
      {
          if($index < -1 || $tag === null || $tag === "")
          {
              return;
          }
          if($this->tag !== 'root')
          {
              return;
          }
          $this->find_and_remove($this, $tag, $index);
      }


然后在simple_html_dom类中加入如下代码

     function remove_node($tag, $index)
     {
         $this->root->remove_node($tag, $index);
     }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值