Fix HTML tags that are not properly closed (supports nesting and close close) code

Source: Internet
Author: User
Tags close close closing tag
  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, there are two modes of "nested closed mode" and
  7. * "Close-up mode" should suffice.
  8. *
  9. * These two patterns are the two nouns I created to explain the implementation of this function,
  10. * Just know what you mean.
  11. * 1, nested closed mode, NEST, as the default closed way. That's "Hello."
  12. * Such HTML code will be modified to "hello"
  13. * 2, close in close mode, this mode will be shaped like "

    How are you doing

    Why not

  14. * Closed "code modified to"

    How are you doing

    Why isn't it closed?

    "
  15. *
  16. * in nested closed mode (default, no special parameters required), you can pass in the nearest closed
  17. * Tag name, in this way will be similar to "

    How are you doing

    I'm good, too. "Convert to

  18. * "

    How are you doing

    I'm fine.

    "In the form.
  19. * The index needs to be written as follows, and 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. * ' xhtmlfix ' = TRUE,
  29. * )
  30. * );
  31. * Fixhtmltag ($param);
  32. *
  33. * The above index corresponds to the following value meaning
  34. * String $html The HTML code to be modified
  35. * Array $tagArray when nested mode is required, the nearest closed tag
  36. * String $type mode name, currently supports NEST and close two modes, if set to close, the parameter $tagArray settings will be ignored, and all the labels close to the nearest
  37. * INI $length If you want to truncate a certain length, you can assign a value here, which refers to the length of the string
  38. * BOOL $lowerTag whether to convert all tags in code to lowercase, by default true
  39. * BOOL $XHtmlFix whether to process labels that do not conform to the XHTML specification and will
    Converted to
  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 XXX
  45. * @param arrays $param array parameters, you need to assign a specific index
  46. * @return string $result the processed HTML code
  47. * @since 2012-04-14
  48. */
  49. function Fixhtmltag ($param = Array ()) {
  50. Default values for parameters
  51. $html = ";
  52. $tagArray = Array ();
  53. $type = ' NEST ';
  54. $length = null;
  55. $lowerTag = TRUE;
  56. $XHtmlFix = TRUE;

  57. First, get a one-dimensional array, $html and $options (if parameters are provided)

  58. Extract ($param);

  59. If there is an options, extract the relevant variables

  60. if (Isset ($options)) {
  61. Extract ($options);
  62. }

  63. $result = "; The final HTML code to return

  64. $tagStack = Array (); Tag stacks, implemented with Array_push () and Array_pop () simulations
  65. $contents = Array (); Used to store HTML tags
  66. $len = 0; The initial length of the string

  67. Sets the closing tag $isClosed, which is true by default, if Close is required, the value is false after the start tag is successfully matched and true after successful closing

  68. $isClosed = true;

  69. All labels to be processed are converted to lowercase

  70. $tagArray = Array_map (' Strtolower ', $tagArray);

  71. "Legal" single-closed label

  72. $singleTagArray = Array (
  73. '
  74. '
  75. '
  76. '
  77. '
  78. '
  79. ');

  80. Verify matching mode $type, default to NEST mode

  81. $type = Strtoupper ($type);
  82. if (!in_array ($type, Array (' NEST ', ' CLOSE '))) {
  83. $type = ' NEST ';
  84. }

  85. Place the strings in the original HTML tags and tags in an array with a pair of < and > delimiters

  86. $contents = Preg_split ("/(<[^>]+?>)/si", $html,-1, Preg_split_no_empty | Preg_split_delim_capture);

  87. foreach ($contents as $tag) {

  88. if (' = = Trim ($tag)) {
  89. $result. = $tag;
  90. Continue
  91. }

  92. A single closed label that matches a standard, such as

  93. if (Preg_match ("/< (\w+) [^\/>]*?\/>/si", $tag)) {
  94. $result. = $tag;
  95. Continue
  96. }

  97. Match the start tag, if it's a single label, out of the stack

  98. else if (Preg_match ("/< (\w+) [^\/>]*?>/si", $tag, $match)) {
  99. If the previous label is not closed and the previous label belongs to the nearest closed type
  100. Then closed, the last label out of the stack

  101. If the label is not closed

  102. if (false = = = $isClosed) {
  103. Close Close mode, closing all labels directly to the nearest
  104. if (' CLOSE ' = = $type) {
  105. $result. = ' ;
  106. Array_pop ($tagStack);
  107. }
  108. Default nesting mode, the label provided by the nearest closing parameter
  109. else {
  110. if (In_array (End ($tagStack), $tagArray)) {
  111. $result. = ' ;
  112. Array_pop ($tagStack);
  113. }
  114. }
  115. }

  116. If the argument $lowerTag true, the label name is converted to lowercase

  117. $matchLower = $lowerTag = = TRUE? Strtolower ($match [1]): $match [1];

  118. $tag = Str_replace (' < '. $match [1], ' < '. $matchLower, $tag);

  119. Start a new label combination
  120. $result. = $tag;
  121. Array_push ($tagStack, $matchLower);

  122. If a single label is a contract, it is closed and the stack is

  123. foreach ($singleTagArray as $singleTag) {
  124. if (Stripos ($tag, $singleTag)!== false) {
  125. if ($XHtmlFix = = TRUE) {
  126. $tag = Str_replace (' > ', '/> ', $tag);
  127. }
  128. Array_pop ($tagStack);
  129. }
  130. }

  131. Close mode, state becomes unclosed

  132. if (' CLOSE ' = = $type) {
  133. $isClosed = false;
  134. }
  135. Default nesting mode, if the label is in the provided $tagArray, the state changes to unclosed
  136. else {
  137. if (In_array ($matchLower, $tagArray)) {
  138. $isClosed = false;
  139. }
  140. }
  141. Unset ($matchLower);
  142. }

  143. Match the closing tag, if appropriate, out of the stack

  144. else if (Preg_match ("/<\/(\w+) [^\/>]*?>/si", $tag, $match)) {

  145. If the argument $lowerTag true, the label name is converted to lowercase

  146. $matchLower = $lowerTag = = TRUE? Strtolower ($match [1]): $match [1];

  147. if (end ($tagStack) = = $matchLower) {

  148. $isClosed = true; Match complete, label closed
  149. $tag = Str_replace (' $result. = $tag;
  150. Array_pop ($tagStack);
  151. }
  152. Unset ($matchLower);
  153. }

  154. Match comments, connect directly $result

  155. else if (Preg_match ("/ /si", $tag)) {
  156. $result. = $tag;
  157. }

  158. Put a string into the $result, by the way the truncation operation

  159. else {
  160. if (Is_null ($length) | | $len + mb_strlen ($tag) < $length) {
  161. $result. = $tag;
  162. $len + = Mb_strlen ($tag);
  163. } else {
  164. $str = mb_substr ($tag, 0, $length-$len + 1);
  165. $result. = $str;
  166. Break
  167. }
  168. }
  169. }

  170. If you also have an unclosed label in the stack connected to the $result

  171. while (!empty ($tagStack)) {
  172. $result. = ' ;
  173. }
  174. return $result;
  175. }
  176. ?>

Copy Code
  • 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.