PHP implementation of HTML tag closure detection and repair method, PHP tag _php tutorial

Source: Internet
Author: User

PHP implementation of HTML tag closure detection and repair method, PHP tags


In this paper, the implementation of HTML tag closure detection and repair method in PHP is described. Share to everyone for your reference. Specific as follows:

HTML tag closure detection and repair, said a bit large, and did not consider a very complete, no use of regular expressions, applicable to the HTML file only the start tag does not have the end tag, there is an end tag is not the beginning of the tag case. The position of the label closure needs to be adjusted according to demand

<?php$str = '"> Content    
 
 

Content full

This is contentThis is content This is cont
  • This is content This is content '; $str _len = strlen ($STR);//record start tag $pre_data = Array ();//record start label position $pre_pos = Array (), $last _data = Array (); $ Error_data = Array (), $error _pos = Array (), $i = 0;//marked < start $start_flag = False;while ($i < $str _len) {if ($str [$i] = = "<" && $str [$i +1]!= '/' && $str [$i +1]!= '! ') {$i + +; $_tmp_str = "; Mark as < start $start _flag = true; Mark Blank $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; View the previous value of the start tag if (count ($pre _data) >0) {$laSt_pre_node = Getlastnode ($pre _data, 1); if ($last _pre_node = = $_tmp_str) {//pairing, delete the corresponding position value Array_pop ($pre _data); Array_pop ($pre _pos); Array_pop ($last _data); } else {//no pairing on, there are two cases//condition one: only closed label, no start tag//Case two: Only start tag, no closed tag 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]== ' > ') {//skips automatic single closed label 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 + +; }}//determines the position of the starting label 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]== < ") {//returns to the original content location $j-; Break } $j + +; } break; } else {$j + +; }} return $j;} Determine the position of the starting label 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;} Gets the inverse of the array of num value function getlastnode (array $arr, $num) {$len = count ($arr); if ($len > $num) {return $arr [$len-$num]; } else {return $arr [0]; }}//collation data, mainly backward looking, further check function sort_data (& $pre _data, & $pre _pos, & $error _data, & $error _pos) {$rem _ Key_array = Array (); $rem _i_array = Array (); Get the value that needs to be deleted 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 }}}//delete the corresponding value of the start tag foreach ($rem _key_array as $_item) {unset ($error _pos[$_item]); Unset ($error _data[$_item]); }//delete end tag corresponding value of foreach ($rem _i_array as $_item) {unset ($pre _data[$_item]); Unset ($pre _pos[$_item]); }}//collation data, closed tag function modify_data ($STR, $pre _data, $pre _pos, $error _data, $error _pos) {$move _log = array (); Only closed label data 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 ']; }//Only start tag data 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_mo Ve_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;} Insert data, $type indicate how the data is inserted function Insert_data ($str, $insert _data, $pos, $type) {$len = strlen ($STR); Start label type 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 = " "; Closed label Type} 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 Lt;= $value; $i + +) {//Echo $str [$i];//}//echo "\ n";//}//foreach ($error _pos as $value) {//for ($i = $value-5; $i <= $value; $i + +) {//Echo $str [$i];//}//echo "\ n";//}?>

    I hope this article is helpful to everyone's PHP programming.

    http://www.bkjia.com/PHPjc/1029591.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029591.html techarticle PHP Implementation of HTML tag closure detection and Repair methods, PHP tags in this paper, the implementation of PHP HTML tag closure detection and repair method. Share to everyone for your reference. Specific as follows: HT ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.