Timber\TextHelper::close_tags PHP Метод

close_tags() публичный статический Метод

public static close_tags ( string $html ) : string
$html string
Результат string
    public static function close_tags($html)
    {
        //put all opened tags into an array
        preg_match_all('#<([a-z]+)(?: .*)?(?<![/|/ ])>#iU', $html, $result);
        $openedtags = $result[1];
        //put all closed tags into an array
        preg_match_all('#</([a-z]+)>#iU', $html, $result);
        $closedtags = $result[1];
        $len_opened = count($openedtags);
        // all tags are closed
        if (count($closedtags) == $len_opened) {
            return $html;
        }
        $openedtags = array_reverse($openedtags);
        // close tags
        for ($i = 0; $i < $len_opened; $i++) {
            if (!in_array($openedtags[$i], $closedtags)) {
                $html .= '</' . $openedtags[$i] . '>';
            } else {
                unset($closedtags[array_search($openedtags[$i], $closedtags)]);
            }
        }
        $html = str_replace(array('</br>', '</hr>', '</wbr>'), '', $html);
        $html = str_replace(array('<br>', '<hr>', '<wbr>'), array('<br />', '<hr />', '<wbr />'), $html);
        return $html;
    }

Usage Example

Пример #1
0
 /**
  * @deprecated since 1.2.0
  * @see TextHelper::close_tags
  * @param string  $html
  * @return string
  */
 public static function close_tags($html)
 {
     return TextHelper::close_tags($html);
 }