最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php實(shí)現(xiàn)html標(biāo)簽閉合檢測(cè)與修復(fù)方法

 更新時(shí)間:2015年07月09日 14:59:14   作者:henosteven  
這篇文章主要介紹了php實(shí)現(xiàn)html標(biāo)簽閉合檢測(cè)與修復(fù)方法,可實(shí)現(xiàn)針對(duì)html標(biāo)簽中結(jié)束標(biāo)簽的檢測(cè)與補(bǔ)全功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了php實(shí)現(xiàn)html標(biāo)簽閉合檢測(cè)與修復(fù)方法。分享給大家供大家參考。具體如下:

html標(biāo)簽閉合檢測(cè)與修復(fù),說的有點(diǎn)大 , 并沒有考慮的很完整,沒有使用正則表達(dá)式, 適用于html文件中只有開始標(biāo)簽沒有結(jié)束標(biāo)簽, 是有結(jié)束標(biāo)簽沒有開始標(biāo)簽的情況。標(biāo)簽閉合的位置需要根據(jù)需求調(diào)整

<?php
$str = '
<div data="<li></li>">
  <img src="http://www.baidu.com/123123.png"/>
  <div2>
    <a>content</a>
  </div2>
    <ul>
      <li>
      </li>
    </ul>
    <p>
    content full
    </p>
    this is content</test1>
    this is content</test2>
    <test4 data="liujinjing"> This is cont
    <li></li>
    <test3 data="liujinjing"> This is content
<div3>
</div3>
</div4>
</div>
</div>
<div6 style="width:90px; "> this is content';
$str_len = strlen($str);
//記錄起始標(biāo)簽
$pre_data = array();
//記錄起始標(biāo)簽位置
$pre_pos = array();
$last_data = array();
$error_data = array();
$error_pos = array();
$i = 0;
//標(biāo)記為 < 開始
$start_flag = false;
while( $i < $str_len ) {
  if($str[$i]=="<" && $str[$i+1]!='/' && $str[$i+1]!='!') {
    $i++;
    $_tmp_str = '';
    //標(biāo)記為 < 開始
    $start_flag = true;
    //標(biāo)記空白
    $space_flag = false;
    while($str[$i]!=">" && $str[$i]!="'" && $str[$i]!='"' && $str[$i] !='/' && $i<$str_len){
      if($str[$i]==' ') {
        $space_flag = true;
      }
      if(!$space_flag) {
        $_tmp_str .= $str[$i];  
      }
      $i++;
    }
    $pre_data[] = $_tmp_str;
    $pre_pos[] = $i;
  } else if ($str[$i]=="<" && $str[$i+1]=='/') {
    $i += 2;
    $_tmp_str = '';
    while($str[$i]!=">" && $i<$str_len){
      $_tmp_str .= $str[$i];
      $i++;
    }
    $last_data[] = $_tmp_str;
    //查看開始標(biāo)簽的上一個(gè)值
    if(count($pre_data)>0) {
      $last_pre_node = getLastNode($pre_data, 1);
      if($last_pre_node == $_tmp_str) {
        //配對(duì)上, 刪除對(duì)應(yīng)位置的值
        array_pop($pre_data);
        array_pop($pre_pos);
        array_pop($last_data);
      } else {
      //沒有配對(duì)上, 有兩種情況
        //情況一: 只有閉合標(biāo)簽, 沒有開始標(biāo)簽
        //情況二:只有開始標(biāo)簽, 沒有閉合標(biāo)簽
        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]=="<" && $str[$i+1]=="!") {
    $i++;
    while($i<$str_len) {
      if($str[$i]=="-" && $str[$i+1]=="-" && $str[$i+2]==">") {
        $i++;
        break;
      } else {
        $i++;
      }
    }
    $i++;
  }else if($str[$i]=='/' && $str[$i+1]=='>') {
    //跳過自動(dòng)單個(gè)閉合標(biāo)簽
    if($start_flag) {
      array_pop($pre_data);
      array_pop($pre_pos);
      $i+=2;
    }
  }else if($str[$i]=="/" && $str[$i+1]=="*"){
    $i++;
    while($i<$str_len) {
      if($str[$i]=="*" && $str[$i+1]=="/") {
        $i++;
        break;
      } else {
        $i++;
    }
    $i++;
  }
  }else if($str[$i]=="'"){
    $i++;
    while($str[$i]!="'" && $i<$str_len) {
      $i++;
    }
    $i++;
  } else if($str[$i]=='"'){
    $i++;
    while($str[$i]!='"' && $i<$str_len ) {
      $i++;
    }
    $i++;
  } else {
    $i++;
  }
}
//確定起始標(biāo)簽的位置
function confirm_pre_pos($str, $pre_pos){
  $str_len = strlen($str);
  $j=$pre_pos;
  while($j < $str_len) {
    if($str[$j] == '"') {
      $j++;
      while ($j<$str_len) {
        if($str[$j]=='"') {
          $j++;
          break;
        }
        $j++;
      }
    }
    else if($str[$j] == "'") {
      $j++;
      while ($j<$str_len) {
        if($str[$j]=="'") {
          $j++;
          break;
        }
        $j++;
      }
    }
    else if($str[$j]==">") {
      $j++;
      while ($j<$str_len) {
        if($str[$j]=="<") {
          //退回到原有內(nèi)容位置
          $j--;
          break;
        }
        $j++;
      }
      break;
    } 
    else {
      $j++;
    }
  }
  return $j;
}
//確定起始標(biāo)簽的位置
function confirm_err_pos($str, $err_pos){
  $j=$err_pos;
  $j--;
  while($j > 0) {
    if($str[$j] == '"') {
      $j--;
      while ($j<$str_len) {
        if($str[$j]=='"') {
          $j--;
          break;
        }
        $j--;
      }
    }
    else if($str[$j] == "'") {
      $j--;
      while ($j<$str_len) {
        if($str[$j]=="'") {
          $j--;
          break;
        }
        $j--;
      }
    }
    else if($str[$j]==">") {
      $j++;
      break;
    } 
    else {
      $j--;
    }
  }
  return $j;
}
//獲取數(shù)組的倒數(shù)第num個(gè)值
function getLastNode(array $arr, $num){
  $len = count($arr);
  if($len > $num) {
    return $arr[$len-$num];
  } else {
    return $arr[0];
  }
}
//整理數(shù)據(jù), 主要是向后看, 進(jìn)一步進(jìn)行檢查
function sort_data(&$pre_data, &$pre_pos, &$error_data, &$error_pos){
  $rem_key_array = array();
  $rem_i_array = array();
  //獲取需要?jiǎng)h除的值
  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;
      }
    }
  }
  //刪除起始標(biāo)簽相應(yīng)的值
  foreach($rem_key_array as $_item) {
    unset($error_pos[$_item]);
    unset($error_data[$_item]);
  }
  //刪除結(jié)束標(biāo)簽相應(yīng)的值
  foreach($rem_i_array as $_item) {
    unset($pre_data[$_item]);
    unset($pre_pos[$_item]);
  }
}
//整理數(shù)據(jù), 閉合標(biāo)簽
function modify_data($str, $pre_data, $pre_pos, $error_data, $error_pos){
  $move_log = array();
  //只有閉合標(biāo)簽的數(shù)據(jù)
  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'];
  }
  //只有起始標(biāo)簽的數(shù)據(jù)
  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;
}
//插入數(shù)據(jù), $type 表示插入數(shù)據(jù)的方式
function insert_data($str, $insert_data, $pos, $type) {
  $len = strlen($str);
  //起始標(biāo)簽類型
  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 = "</" . $insert_data . ">";
  //閉合標(biāo)簽類型
  } 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 = "<" . $insert_data . ">";
  }
  $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<=$value; $i++) {
//     echo $str[$i];
//   }
//   echo "\n";
// }
// foreach($error_pos as $value){
//   for($i=$value-5; $i<=$value; $i++) {
//     echo $str[$i];
//   }
//   echo "\n";
// }
?>

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php之Memcache學(xué)習(xí)筆記

    php之Memcache學(xué)習(xí)筆記

    本篇文章是對(duì)php中的Memcache進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Mysql數(shù)據(jù)庫操作類( 1127版,提供源碼下載 )

    Mysql數(shù)據(jù)庫操作類( 1127版,提供源碼下載 )

    Mysql數(shù)據(jù)庫操作類,學(xué)習(xí)php的朋友可以參考下。
    2010-12-12
  • php生成隨機(jī)數(shù)的三種方法

    php生成隨機(jī)數(shù)的三種方法

    分享下php生成隨機(jī)數(shù)的三種方法,生成1-10之間的不重復(fù)隨機(jī)數(shù),php生成不重復(fù)隨機(jī)數(shù)的例子,需要的朋友參考下
    2014-09-09
  • PHP緩沖區(qū)用法總結(jié)

    PHP緩沖區(qū)用法總結(jié)

    這篇文章主要介紹了PHP緩沖區(qū)用法,結(jié)合實(shí)例形式總結(jié)分析了PHP緩沖區(qū)的功能、使用技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下
    2016-02-02
  • PHP+MySQL+sphinx+scws實(shí)現(xiàn)全文檢索功能詳解

    PHP+MySQL+sphinx+scws實(shí)現(xiàn)全文檢索功能詳解

    這篇文章主要介紹了PHP+MySQL+sphinx+scws實(shí)現(xiàn)全文檢索功能,詳細(xì)分析了sphinx、scws的下載安裝、配置、數(shù)據(jù)庫創(chuàng)建及全文檢索功能的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2019-08-08
  • 將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼

    將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼

    本節(jié)主要介紹了將CMYK顏色值和RGB顏色相互轉(zhuǎn)換的PHP代碼,需要的朋友可以參考下
    2014-07-07
  • 記錄一次排查PHP腳本執(zhí)行卡住的問題

    記錄一次排查PHP腳本執(zhí)行卡住的問題

    最近通過監(jiān)控發(fā)現(xiàn)服務(wù)器中的一臺(tái)機(jī)器異常,通過排除發(fā)現(xiàn)可能是php執(zhí)行腳本出現(xiàn)了問題,于是通過一步步排查,最終解決了這個(gè)問題,下面這篇文章主要是記錄了一次排查PHP腳本執(zhí)行卡住的問題,需要的朋友可以參考下。
    2016-12-12
  • php表單敏感字符過濾類

    php表單敏感字符過濾類

    這篇文章主要介紹了php表單敏感字符過濾類及其用法實(shí)例,詳細(xì)分析了表單生成與提交等環(huán)節(jié)針對(duì)敏感字符的過濾功能,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-12-12
  • 定義php常量的詳解

    定義php常量的詳解

    本篇文章是對(duì)定義php常量進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP內(nèi)核探索:變量存儲(chǔ)與類型使用說明

    PHP內(nèi)核探索:變量存儲(chǔ)與類型使用說明

    這篇文章主要介紹了PHP內(nèi)核探索:變量存儲(chǔ)與類型的相關(guān)資料,需要的朋友可以參考下
    2014-01-01

最新評(píng)論

来安县| 武强县| 塔城市| 宁夏| 中阳县| 花垣县| 广德县| 赫章县| SHOW| 临桂县| 阳曲县| 达日县| 井冈山市| 武安市| 黑龙江省| 太白县| 齐齐哈尔市| 兰西县| 濉溪县| 潼南县| 济宁市| 汝州市| 大新县| 吕梁市| 姚安县| 仲巴县| 巴青县| 沈丘县| 翁牛特旗| 通山县| 崇信县| 普格县| 雅江县| 铁岭市| 揭阳市| 峨边| 伽师县| 隆尧县| 开化县| 佛坪县| 阜宁县|