Why Does DOM Change Encoding?

后端 未结 4 1355
你的背包
你的背包 2020-12-08 03:16
$string = file_get_contents(\'http://example.com\');

if (\'UTF-8\' === mb_detect_encoding($string)) {
    $dom = new DOMDocument();
    // hack to preserve UTF-8 ch         


        
4条回答
  •  孤街浪徒
    2020-12-08 03:49

    I had similar problems recently, and eventually found this workaround - convert all the non-ascii characters to html entities before loading the html

    $string = mb_convert_encoding($string, 'HTML-ENTITIES', "UTF-8");
    $dom->loadHTML($string);
    

提交回复
热议问题