DomDocument and special characters

后端 未结 8 1474
轮回少年
轮回少年 2020-12-14 16:47

This is my code:

$oDom = new DOMDocument();
$oDom->loadHTML(\"èàéìòù\");
echo $oDom->saveHTML();

This is the output:

         


        
8条回答
  •  旧巷少年郎
    2020-12-14 17:00

    Solution:

    $oDom = new DOMDocument();
    $oDom->encoding = 'utf-8';
    $oDom->loadHTML( utf8_decode( $sString ) ); // important!
    
    $sHtml = '';
    $sHtml .= $oDom->saveHTML( $oDom->documentElement ); // important!
    

    The saveHTML() method works differently specifying a node. You can use the main node ($oDom->documentElement) adding the desired !DOCTYPE manually. Another important thing is utf8_decode(). All the attributes and the other methods of the DOMDocument class, in my case, don't produce the desired result.

提交回复
热议问题