Update functions.php to replace UTF-8 hacks by simpler standard approaches

- Replaced charset_hack by standard Unicode UTF-8 BOM;
- Removed removeChild() hack for doctype, and output $doc->documentElement instead;
- Replaced (buggy) saveHTML() by saveXML(), to remove the current overhead of HTML-encoding all non-ASCII characters;
- Removed a redundant trim();

Tested on PHP 5.3.10-1ubuntu3.7.