I`m using tinymce as text editor on my website. Sometimes when I wrote some text and save it the html code is a "broken". It seems there are some开发者_C百科 unclosed html tags which broke the css layout. This is what is found as a solution http://www.php.net/manual/en/tidy.examples.php
Is there some other technique (php) to autocomplete unclosed tags ?
Tidy is the way to go for you. Any custom solution e.g. using regular expressions would be worse than what tidy was designed for.
But the real problem, that TinyMCE is creating unclosed tags sounds like there is some problem with your TinyMCE installation - are you sure you are using the most current version (3.3.9.2)? It should not output unclosed tags, only if you manipulate the HTML source manually.
If you can't use Tidy then you can leverage the DOM to get unclosed and badly nested tags cleaned up. The following will wrap the fixed markup in html and body container tags but you can easily take care of that with a quick preg_replace
or str_replace
.
error_reporting(0);
header('Content-type: text/plain');
$html = '<p>Some <strong><em>badly</strong> formatted content</p>';
$xml = '<?xml version="1.0" encoding="utf-8" ?>' . $html;
$dom = new DomDocument();
$valid = $dom->loadXML($xml);
if (false === $valid) {
$doc = new DOMDocument();
$doc->encoding = 'UTF-8';
$doc->loadHTML($xml);
$html = simplexml_import_dom($doc)->asXML();
}
echo $html;
精彩评论