Php implements html Tag closure detection and repair methods. php tags
This article describes how to implement html Tag closure detection and repair in php. Share it with you for your reference. The details are as follows:
Html Tag closure detection and repair, which is a little big and not fully considered. Regular Expressions are not used. This applies to html files where only the start tag does not end the tag, the end tag does not have a start tag. The location of tag closure needs to be adjusted as needed
<? Php $ str = '<div data = "<li> </li>"> <div2> <a> content </a> </div2> <ul> <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> <div6 style = "width: 90px; "> this is content'; $ str _ Len = strlen ($ str); // record start tag $ pre_data = array (); // record start tag position $ pre_pos = array (); $ last_data = array (); $ error_data = array (); $ error_pos = array (); $ I = 0; // mark as <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) {// pair, delete the value array_pop ($ pre_data); array_pop ($ pre_pos); array_pop ($ last_data);} else {// not paired, there are two situations: // Case 1: only the closed tag, no start tag // Case 2: only the 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] = '> ') {// skip automatic single closed Tag 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 ++;} else if ($ str [$ I] = '"') {$ I ++; while ($ str [$ I]! = '"' & $ I <$ str_len) {$ I ++ ;}$ I ++ ;}else {$ I ++ ;}} // determine the position of the start tag 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] = "<") {// return to the original content location $ j --; break ;} $ j ++;} break;} else {$ j ++;} return $ j;} // determine the position of the start tag. 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 ;} // function getLastNode (array $ arr, $ num) {$ len = count ($ arr); if ($ len> $ num) {return $ arr [$ len-$ num];} else {return $ arr [0] ;}// sort data, mainly to look back, further check function sort_data (& $ pre_data, & $ pre_pos, & $ error_data, & $ error_pos) {$ rem_key_array = array (); $ rem_ I _array = array (); // obtain the value foreach ($ error_data as $ key => $ value) {$ coun T = 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 foreach ($ rem_key_array as $ _ item) {unset ($ error_pos [$ _ item]); unset ($ error_data [$ _ item]);} // Delete the foreach ($ rem_ I _array as $ _ item) {unset ($ pre_data [$ _ item]); unset ($ pre_pos [$ _ item]) ;}// sort data and close the function modify_data ($ str, $ pre_data, $ pre_pos, $ error_data, $ error_pos) {$ move_log = array (); // Only data with closed tags is 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'];} // 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_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;} // insert data, $ type indicates the data insertion method function insert_data ($ str, $ insert_data, $ Pos, $ type) {$ len = strlen ($ str); // start tag 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 = "</". $ insert_data. ">"; // closed tag 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); $ m Id_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); // fo Reach ($ 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"; //}?>
I hope this article will help you with php programming.