php檢測html標籤是否閉合、沒有則修復

夏風1988發表於2017-01-08

本文為大家帶來一篇 php檢測html標籤是否閉合、沒有則修復。希望對您的學習研究PHP有幫助,具體參考描述如下: 

html標籤閉合檢測與修復,說的有點大 , 並沒有考慮的很完整,沒有使用正規表示式, 適用於html檔案中只有開始標籤沒有結束標籤, 是有結束標籤沒有開始標籤的情況。標籤閉合的位置需要根據需求調整

<?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);
//記錄起始標籤
$pre_data = array();
//記錄起始標籤位置
$pre_pos = array();
$last_data = array();
$error_data = array();
$error_pos = array();
$i = 0;
//標記為 < 開始
$start_flag = false;
while( $i < $str_len ) {
if($str[$i]==”<” && $str[$i+1]!=`/` && $str[$i+1]!=`!`) {
$i++;
$_tmp_str = “;
//標記為 < 開始
$start_flag = true;
//標記空白
$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;
//檢視開始標籤的上一個值
if(count($pre_data)>0) {
$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]==”<” && $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]==`>`) {
//跳過自動單個閉合標籤
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++;
}
}
//確定起始標籤的位置
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]==”<“) {
//退回到原有內容位置
$j–;
break;
}
$j++;
}
break;

else {
$j++;
}
}
return $j;
}
//確定起始標籤的位置
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;
}
//獲取陣列的倒數第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 = “</” . $insert_data . “>”;
//閉合標籤型別
} 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 ”
“;
// }
// foreach($error_pos as $value){
// for($i=$value-5; $i<=$value; $i++) {
// echo $str[$i];
// }
// echo ”
“;
// }
?>

 

這裡先講解 php檢測html標籤是否閉合、沒有則修復的內容,有興趣的同學可以繼續瀏覽下面的內容,下一頁會更加精彩哦。

●【往下看,下一頁更精彩】●
1  2  3  4  5  6  7  下一頁 


延伸閱讀:
·php正規表示式替換HTML標籤
·PHP過濾HTML標籤的實現方法
·php過濾提交表單的html標籤
·PHP實現刪除HTML標籤,得到純文字(可以處理巢狀的標籤)
·php判斷字串是否有html標籤
·PHP淺析HTML標籤過濾方法
·php實現批量替換html標籤
·PHP清除HTML標籤的三種方式
·PHP清除HTML標籤的實現方法
·php使用正則替換html標籤和樣式的函式
·php使用正規表示式過濾html標籤、空格、換行符
·smarty實現過濾html標籤後再擷取文章
·php使用正則過濾HTML標籤、屬性示例
·php擷取含html標籤的字串示例
·php去除字串中的HTML標籤示例

相關文章