force_balance_tags() WP 2.0.4

Виправляє неправильні XHTML/HTML теги у тексті: не закриті, не по порядку, неправильний синтаксис ( <br >а не <br />).

Закриває всі незакриті HTML-теги у вказаному рядку, виправляє помилки у неправильному порядку тегів.

На відміну від balanceTags() , ігнорує опцію use_balanceTags.

За замовчуванням WP застосовується до тексту коментаря:

remove_filter( 'comment_text', 'force_balance_tags', 25);

Основа для:
balanceTags()
1 раз – 0.00025 сек
(швидко) | 50000 разів – 2.65 сек
(швидко) |
PHP 7.1.11, WP 4.9.8

Хуків немає.

Повертає

Строку. виправлений рядок.

Використання

force_balance_tags($text);
$text
(рядок) (обов’язковий)
Текст, який слід виправити (збалансувати теги).

Приклади

0

#1 Приклад виправлення неправильного HTML

Приклад того, як функція змінює місцями неправильно використані html теги і як закриває теги, які не були закриті:

$text = "<p><b>Це текст, в якому</p></b> html теги не збалансовані.
<p>Тобто. є які відкриваються, але потім не закриваються, що може
призвести до проблем. Використовуємо функцію <strong>balanceTags().
</div><!--просто закритий тег, але не відкритий - зайвий-->";

echo htmlspecialchars( force_balance_tags($text) );

/*
Отримаємо:

<p><b>Це текст, в якому</b></p> html теги не збалансовані.
<p>Тобто. є які відкриваються, але потім не закриваються, що може
призвести до проблем. Використовуємо функцію <strong>balanceTags().
<!--просто закритий тег, але не відкритий - зайвий--></strong></p>
*/

список змін

З версії 2.0.4Введено.
З версії 5.3.0Improve accuracy and add support для custom element tags.

force balance tags WP 6.0.2

function force_balance_tags( $text ) { $tagstack = array(); $ stacksize = 0; $tagqueue = ''; $newtext = ''; // Known single-entity/self-closing tags. $single_tags = array( 'area', 'base', 'basefont', 'br', 'col', 'command', 'embed', 'frame', 'hr', 'img', 'input', ' isindex', 'link', 'meta', 'param', 'source', 'track', 'wbr'); // Tags, які можуть бути безперервно невтішними з ними. $nestable_tags = array( 'article', 'aside', 'blockquote', 'details', 'div', 'figure', 'object', 'q', 'section', 'span' ); // WP bug fix for comments - у випадку ви дійсно маєте тип '< !--'. $text = str_replace( '< !--', '< !--', $text ); // WP bug fix for LOVE <3 (і інших положень з '<' before a number). $text = preg_replace( '#<([0-9]{1})#', '<$1', $text ); /** * Matches supported tags. * * Щоб отримати pattern як string без коментарів paste into a PHP * REPL like `php -a`. * * @see https://html.spec.whatwg.org/#elements-2 * @see https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name * * @example * ~# php -a * php > $s = [paste copied contents of expression below including parentheses]; * php > echo $s; */ $tag_pattern = ( '#<'. // Start with an opening bracket. '(/?)'. // Group 1 - If it's a closing tag it'll має leading slash. '('. // Group 2 - Tag name. // Custom element tags have more lenient rules than HTML tag names. '(?:[az](?:[a-z0-9._]*)-(?:[a-z0-9._-]+)+)' . '|' . // Traditional tag rules approximate HTML tag names. '(?:[w:]+)' . ')'. '(?:' . // We either immediately close the tag with its '>' і не має. 's*' . '(/?)'. // Group 3 - "attributes" for empty tag. '|' . // або ми повинні start with space characters to separate the tag name from the attributes (or whitespace). '(s+)' . // Group 4 - Pre-attribute whitespace. '([^>]*)' . // Group 5 - Attributes. ')'. '>#' // End with a closing bracket. ); while ( preg_match( $tag_pattern, $text, $regex ) ) { $full_match = $regex[0]; $has_leading_slash = ! empty ($ Regex [1]); $tag_name = $regex[2]; $tag = strtolower( $tag_name ); $ is_single_tag = in_array ($ tag, $ single_tags, true); $pre_attribute_ws = isset( $regex[4] ) ? $regex[4] : ''; $attributes = trim( isset( $regex[5] ) ? $regex[5] : $regex[3] ); $has_self_closer = '/' === substr( $attributes, -1 ); $newtext. = $tagqueue; $i = strpos($text, $full_match); $l = strlen ($ full_match); // Clear the shifter. $tagqueue = ''; if ($ has_leading_slash) { // End tag. // If too many closing tags. if ( $stacksize <= 0 ) { $tag = ''; // Or close to be safe $tag = '/'. $tag. // If stacktop value = tag close value, then pop. } elseif ($ tagstack [$ stacksize - 1] === $ tag) { // Found closing tag. $tag = '</'. $tag. '>'; // Close tag. array_pop ($ tagstack); $stacksize--; } else { // Closing tag not at top, search for it. for ($j = $stacksize - 1; $j> = 0; $j--) { if ( $tagstack[ $j ] === $tag ) { // Add tag to tagqueue. for ($k = $stacksize - 1; $k> = $j; $k--) { $tagqueue .= '</' . array_pop ($ tagstack). '>'; $stacksize--; } break; } } $tag = ''; } } else { // Begin tag. if ( $has_self_closer ) { // If it presents itself as a self-closing tag... // ...but it isn't a known single-entity self-closing tag, don't let it be treated as such // and immediately close it with a closing tag (the tag will encapsulate no text as a result). if ( ! $is_single_tag ) { $ attributes = trim (substr ($ attributes, 0, -1)). "></$tag"; } } elseif ( $ is_single_tag ) { // Else if it's a known single-entity tag but it it close itself, do so. $pre_attribute_ws = ''; $attributes .= '/'; } else { // It's not a single-entity tag. // Якщо вгорі шлюпка є той самий як tag, ми будемо писати, close previous tag. if ( $stacksize > 0 && ! in_array( $tag, $nestable_tags, true ) && $tagstack[ $stacksize - 1 ] === $tag ) { $tagqueue = '</'. array_pop ($ tagstack). '>'; $stacksize--; } $ stacksize = array_push ($ tagstack, $ tag); } // Attributes. if ( $has_self_closer && $is_single_tag ) { // We need some space - avoid <br /> і prefer <br />. $pre_attribute_ws = ''; } $tag = '<'. $tag. $pre_attribute_ws . $attributes . '>'; // If already queuing a close tag, put this tag on too. if ( ! empty( $tagqueue ) ) { $tagqueue. = $tag; $tag = ''; } } $newtext .= substr( $text, 0, $i ) . $tag; $text = substr($text, $i + $l); } // Clear tag queue. $newtext. = $tagqueue; //Add remaining text. $newtext.=$text; while ( $ x = array_pop ( $ tagstack ) ) { $newtext .= '</' . $x. '>'; // Add remaining tags to close. } // WP fix for the bug with HTML comments. $newtext = str_replace( '< !--', '<!--', $newtext ); $newtext = str_replace( '< !--', '< !--', $newtext ); return $newtext; }

Залишити відповідь

Ваша e-mail адреса не оприлюднюватиметься. Обов’язкові поля позначені *