找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 887|回复: 0
打印 上一主题 下一主题

php实现html标签闭合检测与修复方法

[复制链接]

2560

主题

2560

帖子

7622

积分

论坛元老

Rank: 8Rank: 8

积分
7622
跳转到指定楼层
楼主
发表于 2018-2-14 05:44:15 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            本文实例讲述了php实现html标签闭合检测与修复方法。分享给大家供大家参考。具体如下:
html标签闭合检测与修复,说的有点大 , 并没有考虑的很完整,没有使用正则表达式, 适用于html文件中只有开始标签没有结束标签, 是有结束标签没有开始标签的情况。标签闭合的位置需要根据需求调整
">
  

  
    content
  
   
      

  •       
       

       
        content full
       
        this is content
        this is content
         This is cont
       

  •      This is content
    this is content';
    $str_len = strlen($str);
    //记录起始标签
    $pre_data = array();
    //记录起始标签位置
    $pre_pos = array();
    $last_data = array();
    $error_data = array();
    $error_pos = array();
    $i = 0;
    //标记为 " && $str[$i]!="'" && $str[$i]!='"' && $str[$i] !='/' && $i" && $i0) {
          $last_pre_node = getLastNode($pre_data, 1);
          if($last_pre_node == $_tmp_str) {
            //配对上, 删除对应位置的值
            array_pop($pre_data);
            array_pop($pre_pos);
            array_pop($last_data);
          } else {
          //没有配对上, 有两种情况
            //情况一: 只有闭合标签, 没有开始标签
            //情况二:只有开始标签, 没有闭合标签
            array_pop($last_data);
            $error_data[] = $_tmp_str;
            $error_pos[] = $i;
          }
        } else {
            array_pop($last_data);
            $error_data[] = $_tmp_str;
            $error_pos[] = $i;      
        }
      }else if ($str[$i]=="") {
            $i++;
            break;
          } else {
            $i++;
          }
        }
        $i++;
      }else if($str[$i]=='/' && $str[$i+1]=='>') {
        //跳过自动单个闭合标签
        if($start_flag) {
          array_pop($pre_data);
          array_pop($pre_pos);
          $i+=2;
        }
      }else if($str[$i]=="/" && $str[$i+1]=="*"){
        $i++;
        while($i") {
          $j++;
          while ($j 0) {
        if($str[$j] == '"') {
          $j--;
          while ($j") {
          $j++;
          break;
        }
        else {
          $j--;
        }
      }
      return $j;
    }
    //获取数组的倒数第num个值
    function getLastNode(array $arr, $num){
      $len = count($arr);
      if($len > $num) {
        return $arr[$len-$num];
      } else {
        return $arr[0];
      }
    }
    //整理数据, 主要是向后看, 进一步进行检查
    function sort_data(&$pre_data, &$pre_pos, &$error_data, &$error_pos){
      $rem_key_array = array();
      $rem_i_array = array();
      //获取需要删除的值
      foreach($error_data as $key=>$value){
        $count = count($pre_data);
        for($i=($count-1) ; $i>=0; $i--) {
          if($pre_data[$i] == $value && !in_array($i, $rem_i_array)) {
            $rem_key_array[] = $key;
            $rem_i_array[] = $i;
            break;
          }
        }
      }
      //删除起始标签相应的值
      foreach($rem_key_array as $_item) {
        unset($error_pos[$_item]);
        unset($error_data[$_item]);
      }
      //删除结束标签相应的值
      foreach($rem_i_array as $_item) {
        unset($pre_data[$_item]);
        unset($pre_pos[$_item]);
      }
    }
    //整理数据, 闭合标签
    function modify_data($str, $pre_data, $pre_pos, $error_data, $error_pos){
      $move_log = array();
      //只有闭合标签的数据
      foreach ($error_data as $key => $value) {
        // code...
        $_tmp_move_count = 0;
        foreach ($move_log as $pos_key => $move_value) {
          // code...
          if($error_pos[$key]>=$pos_key) {
            $_tmp_move_count += $move_value;
          }
        }
        $data = insert_data($str, $value, $error_pos[$key]+$_tmp_move_count, false);
        $str = $data['str'];
        $move_log[$data['pos']] = $data['move_count'];
      }
      //只有起始标签的数据
      foreach ($pre_data as $key => $value) {
        // code...
        $_tmp_move_count = 0;
        foreach ($move_log as $pos_key => $move_value) {
          // code...
          if($pre_pos[$key]>=$pos_key) {
            $_tmp_move_count += $move_value;
          }
        }
        $data = insert_data($str, $value, $pre_pos[$key]+$_tmp_move_count, true);
        $str = $data['str'];
        $move_log[$data['pos']] = $data['move_count'];
      }
      return $str;
    }
    //插入数据, $type 表示插入数据的方式
    function insert_data($str, $insert_data, $pos, $type) {
      $len = strlen($str);
      //起始标签类型
      if($type==true) {
        $move_count = strlen($insert_data)+3;
        $pos = confirm_pre_pos($str, $pos);
        $pre_str = substr($str, 0, $pos);
        $end_str = substr($str, $pos);
        $mid_str = "";
      //闭合标签类型
      } else {
        $pos = confirm_err_pos($str, $pos);
        $move_count = strlen($insert_data) + 2;
        $pre_str = substr($str, 0, $pos);
        $end_str = substr($str, $pos);
        $mid_str = "";
      }
      $str = $pre_str.$mid_str.$end_str;
      return array('str'=>$str, 'pos'=>$pos, 'move_count'=>$move_count);
    }
    sort_data($pre_data, $pre_pos, $error_data, $error_pos);
    $new_str = modify_data($str, $pre_data, $pre_pos, $error_data, $error_pos);
    echo $new_str;
    // print_r($pre_data);
    // print_r($pre_pos);
    // print_r($error_data);
    // print_r($error_pos);
    // echo strlen($str);
    // foreach($pre_pos as $value){
    //   $value = confirm_pre_pos($str, $value);
    //   for($i=$value-5; $i
    希望本文所述对大家的php程序设计有所帮助。
                
                
    您可能感兴趣的文章:
  • php 去除html标记--strip_tags与htmlspecialchars的区别详解
  • PHP中HTML标签过滤技巧
  • php获取网页标题和内容函数(不包含html标签)
  • PHP strip_tags()去除HTML、XML以及PHP的标签介绍
  • PHP函数strip_tags的一个bug浅析
  • PHP关于htmlspecialchars、strip_tags、addslashes的解释
  • php实现过滤表单提交中html标签的方法
  • php截取html字符串及自动补全html标签的方法
  • PHP实现过滤各种HTML标签
  • PHP正则表达式过滤html标签属性(DEMO)
  • PHP strip_tags() 去字符串中的 HTML、XML 以及 PHP 标签的函数
  • PHP strip_tags保留多个HTML标签的方法
  • php使用strip_tags()去除html标签仍有空白的解决方法
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端