服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php实现html标签闭合检测与修复方法

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

2020-11-03 23:01henosteven PHP教程

这篇文章主要介绍了php实现html标签闭合检测与修复方法,可实现针对html标签中结束标签的检测与补全功能,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了html">php实现html标签闭合检测与修复方法。分享给大家供大家参考。具体如下:

html标签闭合检测与修复,说的有点大 , 并没有考虑的很完整,没有使用正则表达式, 适用于html文件中只有开始标签没有结束标签, 是有结束标签没有开始标签的情况。标签闭合的位置需要根据需求调整

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?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 "\n";
// }
// foreach($error_pos as $value){
//   for($i=$value-5; $i<=$value; $i++) {
//     echo $str[$i];
//   }
//   echo "\n";
// }
?>

希望本文所述对大家的php程序设计有所帮助。

延伸 · 阅读

精彩推荐