Fix code for HTML tags that are not properly closed (nested and close proximity are supported)

Source: Internet
Author: User
Fix code for HTML tags that are not properly closed (nested and close proximity are supported)

  1. /**

  2. * FixHtmlTag
  3. *
  4. * Html tag repair function. This function can fix HTML tags that are not properly closed.
  5. *
  6. * Due to too many uncertainties, the nested closed mode and
  7. * "Nearby close mode" should be enough.
  8. *
  9. * These two modes are the two terms I created to explain the implementation of this function,
  10. * You only need to understand what it means.
  11. * 1. nested closed mode, NEST, which is the default closed mode. That is"

    Hello"

  12. * Such html code will be changed"

    Hi!

    "
  13. * 2. CLOSE mode: CLOSE mode. this mode will be like"

    Hi!

    Why not

  14. * Close it. "change the code"

    Hi!

    Why is it not closed?

    "
  15. *
  16. * In nested close mode (by default, special parameters are not required), you can input the values that need to be closed nearby.
  17. * Tag name, which is similar"

    Hi!

    Convert me

  18. *"

    Hi!

    Me too

    "Format.
  19. * When passing parameters, the index must be written as follows. the settings that do not need to be modified can be omitted.
  20. *
  21. * $ Param = array (
  22. * 'Html' => '', // required
  23. * 'Options' => array (
  24. * 'Tagarray' => array ();
  25. * 'Type' => 'nest ',
  26. * 'Length' => null,
  27. * 'Lowertag' => TRUE,
  28. * 'Htmlfix' => TRUE,
  29. *)
  30. *);
  31. * FixHtmlTag ($ param );
  32. *
  33. * The meanings of the index values are as follows:
  34. * String $ html code to be modified
  35. * Array $ tagArray when nested mode is used, the near closed tag array is required.
  36. * String $ type mode name. Currently, NEST and CLOSE modes are supported. if it is set to CLOSE, the $ tagArray parameter is ignored, and all labels are closed nearby.
  37. * Ini $ length if you want to truncation a certain length, you can assign a value here. this length refers to the string length.
  38. * Whether the bool $ lowerTag converts all tags in the code to lowercase. the default value is TRUE.
  39. * Bool $ whether XHtmlFix processes tags that do not comply with XHTML specifications
    Convert
  40. *
  41. * @ Author IT Tumbler
  42. * @ Version 0.2
  43. * @ Link http://bbs.it-home.org IT Tumbler
  44. * @ Link: http://enenba.com /? Post = 19 xx
  45. * @ Param array $ param array parameter, which must be assigned a specific index
  46. * @ Return string $ html code after result processing
  47. * @ Since 2012-04-14
  48. */
  49. Function fixHtmlTag ($ param = array ()){
  50. // Default value of the parameter
  51. $ Html = '';
  52. $ TagArray = array ();
  53. $ Type = 'nest ';
  54. $ Length = null;
  55. $ LowerTag = TRUE;
  56. $ XHtmlFix = TRUE;

  57. // First obtain the one-dimensional array, that is, $ html and $ options (if parameters are provided)

  58. Extract ($ param );

  59. // Extract related variables if options exists

  60. If (isset ($ options )){
  61. Extract ($ options );
  62. }

  63. $ Result = ''; // The html code to be returned.

  64. $ TagStack = array (); // tag stack, simulated using array_push () and array_pop ()
  65. $ Contents = array (); // used to store html tags
  66. $ Len = 0; // initial string length

  67. // Set the closure Flag $ isClosed. the default value is TRUE. if you need to close the tag nearby, the value is false after the start tag is successfully matched, and true after the tag is closed.

  68. $ IsClosed = true;

  69. // Convert all tags to be processed to lowercase.

  70. $ TagArray = array_map ('strtolower ', $ tagArray );

  71. // "Valid" single closed tag

  72. $ SingleTagArray = array (
  73. ' ' ' ' ' ' ');

  74. // Check the matching mode $ type. the default mode is NEST.

  75. $ Type = strtoupper ($ type );
  76. If (! In_array ($ type, array ('nest ', 'close '))){
  77. $ Type = 'nest ';
  78. }

  79. // Use a pair of <and> as the separator to put the original html tag and the string in the tag into the array

  80. $ Contents = preg_split ("/(<[^>] +?>) /Si ", $ html,-1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );

  81. Foreach ($ contents as $ tag ){

  82. If (''= trim ($ tag )){
  83. $ Result. = $ tag;
  84. Continue;
  85. }

  86. // Matches standard single-closed tags, such

  87. If (preg_match ("/<(\ w +) [^ \/>] *? \/>/Si ", $ tag )){
  88. $ Result. = $ tag;
  89. Continue;
  90. }

  91. // Match the start tag. if it is a single tag, the stack is output.

  92. Else if (preg_match ("/<(\ w +) [^ \/>] *?> /Si ", $ tag, $ match )){
  93. // If the last tag is not closed and the last tag belongs to the nearest closed type
  94. // Close. The previous tag is output from the stack.

  95. // If the tag is not closed

  96. If (false ===$ isClosed ){
  97. // Close all labels in proximity mode.
  98. If ('close' = $ type ){
  99. $ Result. =' ';
  100. Array_pop ($ tagStack );
  101. }
  102. // The default nested mode, which is the tag provided by the nearby closed parameter
  103. Else {
  104. If (in_array (end ($ tagStack), $ tagArray )){
  105. $ Result. =' ';
  106. Array_pop ($ tagStack );
  107. }
  108. }
  109. }

  110. // If the parameter $ lowerTag is TRUE, convert the tag name to lowercase.

  111. $ MatchLower = $ lowerTag = TRUE? Strtolower ($ match [1]): $ match [1];

  112. $ Tag = str_replace ('<'. $ match [1], '<'. $ matchLower, $ tag );

  113. // Start a new tag combination
  114. $ Result. = $ tag;
  115. Array_push ($ tagStack, $ matchLower );

  116. // If it belongs to the agreed single tag, close it and exit the stack

  117. Foreach ($ singleTagArray as $ singleTag ){
  118. If (stripos ($ tag, $ singleTag )! = False ){
  119. If ($ XHtmlFix = TRUE ){
  120. $ Tag = str_replace ('>', '/>', $ tag );
  121. }
  122. Array_pop ($ tagStack );
  123. }
  124. }

  125. // Close nearby mode. the status changes to unclosed

  126. If ('close' = $ type ){
  127. $ IsClosed = false;
  128. }
  129. // Default nesting mode. if the tag is in the $ tagArray provided, the status is changed to unclosed.
  130. Else {
  131. If (in_array ($ matchLower, $ tagArray )){
  132. $ IsClosed = false;
  133. }
  134. }
  135. Unset ($ matchLower );
  136. }

  137. // Match the closed tag. if applicable, the tag is output to the stack.

  138. Else if (preg_match ("/<\/(\ w +) [^ \/>] *?> /Si ", $ tag, $ match )){

  139. // If the parameter $ lowerTag is TRUE, convert the tag name to lowercase.

  140. $ MatchLower = $ lowerTag = TRUE? Strtolower ($ match [1]): $ match [1];

  141. If (end ($ tagStack) ==$ matchLower ){

  142. $ IsClosed = true; // the matching is complete and the tag is closed.
  143. $ Tag = str_replace (' $ Result. = $ tag;
  144. Array_pop ($ tagStack );
  145. }
  146. Unset ($ matchLower );
  147. }

  148. // Match the comments and connect directly to $ result

  149. Else if (preg_match ("/ /Si ", $ tag )){
  150. $ Result. = $ tag;
  151. }

  152. // Put the string into $ result and perform the truncation operation.

  153. Else {
  154. If (is_null ($ length) | $ len + mb_strlen ($ tag) <$ length ){
  155. $ Result. = $ tag;
  156. $ Len + = mb_strlen ($ tag );
  157. } Else {
  158. $ Str = mb_substr ($ tag, 0, $ length-$ len + 1 );
  159. $ Result. = $ str;
  160. Break;
  161. }
  162. }
  163. }

  164. // If you still need to connect unclosed tags in the stack to $ result

  165. While (! Empty ($ tagStack )){
  166. $ Result. =' ';
  167. }
  168. Return $ result;
  169. }
  170. ?>

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.