DANFA

Метод закрытия незакрытых BBcode

PHP
Пару лет назад нашел функцию закрытия незакрытых HTML тегов. Переписал под свои нужды метод для закрытия незакрытых BBcode. Возможно, кому то пригодиться:
	public function closeAllBBcode($html)
	{
		preg_match_all('#\[([a-z]+)\]#iU', $html, $result);
		$openedTags = $result[1];

		preg_match_all('#\[/([a-z]+)\]#iU', $html, $result);

		$closedTags = $result[1];

		$lenOpened = count($openedTags);
		if (count($closedTags) == $lenOpened)
		{
			return $html;
		}

		$openedTags = array_reverse($openedTags);
		for ($i = 0; $i < $lenOpened; $i++)
		{
			if (!in_array($openedTags[$i], $closedTags))
			{
				$html .= '[/' . $openedTags[$i] . ']';

			}
			else
			{
				unset($closedTags[array_search($openedTags[$i], $closedTags)]);
			}
		}

		return $html;
	}

Читайте еще: Метод закрытия незакрытых HTML тегов.