Skip to content
Advertisement

converted to when using PHP DOMDocument

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.

<?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:

<!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().

$html = preg_replace('/<(/?)([a-z]+):/', '<$1$2___', $html);
$doc->loadHTML($html);
// do stuff
$html = preg_replace('/<(/?)([a-z]+)___/', '<$1$2:', $doc->saveHtml());
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement