diff --git a/base.php b/base.php index 7c4f9c7..56503c6 100644 --- a/base.php +++ b/base.php @@ -20,6 +20,24 @@ function getUrlWithVersion ($url) { return $url . "?v=" . VERSION; } +function xml2xhtml($xml) { + return preg_replace_callback('#<(\w+)([^>]*)\s*/>#s', create_function('$m', ' + $xhtml_tags = array("br", "hr", "input", "frame", "img", "area", "link", "col", "base", "basefont", "param"); + return in_array($m[1], $xhtml_tags) ? "<$m[1]$m[2] />" : "<$m[1]$m[2]>"; + '), $xml); +} + +function html2xhtml ($html) { + $doc = new DOMDocument(); + $doc->loadHTML($html); // Load the HTML + $output = utf8_decode($doc->saveXML($doc->documentElement)); // Transform to an Ansi xml stream + $output = xml2xhtml($output); // Fix the br / hr ... + if (preg_match ("/(.*)<\/body><\/html>/", $output, $matches)) { + $output = $matches [1]; // Remove + } + return $output; +} + /** * This method is a direct copy-paste from * http://tmont.com/blargh/2010/1/string-format-in-php diff --git a/book.php b/book.php index 17b184a..9e6ce17 100644 --- a/book.php +++ b/book.php @@ -254,7 +254,7 @@ class Book extends Base { } if (preg_match ("/<\/(div|p|a)>/", $this->comment)) { - return $addition . preg_replace ("/<(br|hr)>/", "<$1 />", $this->comment); + return $addition . html2xhtml ($this->comment); } else {