Php html tag closure detection and repair methods

Source: Internet
Author: User
This article mainly introduces how to implement html tag closure detection and repair in php. it can detect and complete html tag termination, which is of great practical value, for more information about how to implement html tag closure detection and repair in php, see the following example. 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 ='

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 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'];} // only the data of the start label 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 =" "; // 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); $ 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, $ p Re_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"; //}?>

    I hope this article will help you with php programming.

    Related Article

    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.