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)

Jul 25, 2016 am 09:05 AM

  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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles