Home > Backend Development > PHP Tutorial > Fix code for HTML tags that are not closed normally (supports nesting and nearby closing)

Fix code for HTML tags that are not closed normally (supports nesting and nearby closing)

WBOY
Release: 2016-07-25 09:05:16
Original
1190 people have browsed it
  1. /**

  2. * fixHtmlTag
  3. *
  4. * html标签修复函数,此函数可以修复未正确闭合的 HTML 标签
  5. *
  6. * 由于不确定性因素太多,暂时提供两种模式“嵌套闭合模式”和
  7. * “就近闭合模式”,应该够用了。
  8. *
  9. * 这两种模式是我为了解释清楚此函数的实现而创造的两个名词,
  10. * 只需明白什么意思就行。
  11. * 1,嵌套闭合模式,NEST,为默认的闭合方式。即 "
    你好"
  12. * 这样的 html 代码会被修改为 "
    你好
    "
  13. * 2. Nearby closing mode, CLOSE. This mode will modify the code in the form of "

    Hello

    Why is there no

  14. * closed?" to "

    Why is it not closed

    "
  15. *
  16. * In the nested closing mode (default, no special parameters are required), you can pass in the
  17. * that needs to be closed nearby Tag name, in this way, something like "

    Hello

    Me, too" will be converted into

  18. * "

    Hello< /p>

    I also like the form of

    ".
  19. * When passing parameters, the index needs to be written as follows. 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 meaning of the value corresponding to the above index is as follows
  34. * string $html The html code that needs to be modified
  35. * array $tagArray shall be When nesting mode, the nearest closed tag array is required
  36. * string $type mode name, currently supports two modes: NEST and CLOSE. If set to CLOSE, the setting of the parameter $tagArray will be ignored, and all tags will be closed at the nearest
  37. * ini $length If you want to truncate a certain length, you can assign a value here. This length refers to the length of the string.
  38. * bool $lowerTag Whether to convert all tags in the code to lowercase, the default is TRUE
  39. * bool $XHtmlFix Whether to handle inconsistencies XHTML standard tags, that is, converting
    to
  40. *
  41. * @author IT卤场
  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 needs to be assigned a specific index
  46. * @return string $result processed html code
  47. * @since 2012-04-14
  48. */
  49. function fixHtmlTag($param = array()) {
  50. //Default value of parameter
  51. $html = '';
  52. $tagArray = array();
  53. $ type = 'NEST';
  54. $length = null;
  55. $lowerTag = TRUE;
  56. $XHtmlFix = TRUE;

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

  58. extract($param);

  59. //If options exist, extract relevant variables

  60. if (isset($options)) {
  61. extract($options);
  62. }< ;/p>
  63. $result = ''; //The final html code to be returned

  64. $tagStack = array(); //Tag stack, simulated with array_push() and array_pop()
  65. $contents = array(); //Used to store html tags
  66. $len = 0; //Initial length of the string

  67. //Set the closing mark $isClosed, the default is TRUE, if you need to close it nearby , after successfully matching the start tag, its value is false, and after successful closing, it is true

  68. $isClosed = true;

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

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

  71. //"Legal" single closed tag

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

  81. //Verification matching pattern $type , the default is NEST mode

  82. $type = strtoupper($type);
  83. if (!in_array($type, array('NEST', 'CLOSE'))) {
  84. $type = 'NEST';
  85. }
  86. //Using a pair of < and > as delimiters, put the original html tag and the string in the tag into an array

  87. $contents = preg_split("/(<[^> ]+?>)/si", $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);

  88. foreach ($contents as $tag) {

  89. if ('' == trim($ tag)) {
  90. $result .= $tag;
  91. continue;
  92. }

  93. //Match standard single closed tags, such as

  94. if (preg_match("/ <(w+)[^/>]*?/>/si", $tag)) {
  95. $result .= $tag;
  96. continue;
  97. }

  98. // Match the start tag, if it is a single tag, pop it off the stack

  99. else if (preg_match("/<(w+)[^/>]*?>/si", $tag, $match)) {
  100. //if The previous label is not closed, and the previous label belongs to the nearest closed type
  101. //Close it, and pop the previous label

  102. //If the label is not closed

  103. if (false === $ isClosed) {
  104. //Close close mode, close all tags directly
  105. if ('CLOSE' == $type) {
  106. $result .= '';
  107. array_pop($tagStack);
  108. }
  109. //Default nesting mode, the tag provided by the nearest closing parameter
  110. else {
  111. if (in_array(end($tagStack), $tagArray)) {
  112. $result .= '< ;/' . end($tagStack) . '>';
  113. array_pop($tagStack);
  114. }
  115. }
  116. }

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

  118. $matchLower = $lowerTag == TRUE ? strtolower($match[1]) : $match[1];

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

  120. //Start a new tag combination
  121. $result . = $tag;
  122. array_push($tagStack, $matchLower);

  123. //If it belongs to the agreed single tag, close it and pop it out of the stack

  124. foreach ($singleTagArray as $singleTag) {
  125. if (stripos($tag, $singleTag) !== false) {
  126. if ($XHtmlFix == TRUE) {
  127. $tag = str_replace('>', ' />', $tag);
  128. }
  129. array_pop($tagStack);
  130. }
  131. }

  132. //Nearby closed mode, status changes to unclosed

  133. if ('CLOSE' == $type) {
  134. $isClosed = false;
  135. }
  136. //Default nesting mode, if the tag is located in the provided $tagArray, the status is changed to unclosed
  137. else {
  138. if (in_array($matchLower, $tagArray)) {
  139. $isClosed = false;
  140. }
  141. }
  142. unset($matchLower);
  143. }

  144. //Match the closing tag and pop it off the stack if appropriate

  145. else if (preg_match("/ ]*?>/si", $tag, $match)) {

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

  147. $matchLower = $lowerTag == TRUE ? strtolower($match[1]) : $match[1];

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

  149. $isClosed = true; //Match completed , tag closure
  150. $tag = str_replace('
  151. $result .= $tag;
  152. array_pop($tagStack);
  153. }
  154. unset($matchLower);
  155. }

  156. //Match comments, directly connect $result

  157. else if (preg_match("// si", $tag)) {
  158. $result .= $tag;
  159. }

  160. //Put the string into $result and do the truncation operation

  161. else {
  162. if (is_null ($length) || $len + mb_strlen($tag) < $length) {
  163. $result .= $tag;
  164. $len += mb_strlen($tag);
  165. } else {
  166. $str = mb_substr($ tag, 0, $length - $len + 1);
  167. $result .= $str;
  168. break;
  169. }
  170. }
  171. }

  172. //If there are still unused items in the stack The closed tag is connected to $result

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

Copy code


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template