I’ve the following HTML code with a custom tag <gcse:search>
. It’s from “Custom Google Search Engine” to embed in a page.
However, after parsing via PHP DOMDocument, <gcse:search>
gets converted to <search
> breaking the functionality.
JavaScript
x
<?php
$html = <<<EOD
<!DOCTYPE html>
<html>
<body>
<gcse:search enablehistory="false"></gcse:search>
</body>
</html>
EOD;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML($html);
echo $dom->saveHTML();
Output:
JavaScript
<!DOCTYPE html>
<html>
<body>
<search enablehistory="false"></search>
</body>
</html>
Advertisement
Answer
The only solution I found is to replace :
with ___
and then replace back after saveHTML()
.
JavaScript
$html = preg_replace('/<(/?)([a-z]+):/', '<$1$2___', $html);
$doc->loadHTML($html);
// do stuff
$html = preg_replace('/<(/?)([a-z]+)___/', '<$1$2:', $doc->saveHtml());