代码之家  ›  专栏  ›  技术社区  ›  step

DOMDocument saveHTML输出错误

  •  1
  • step  · 技术社区  · 7 年前

    我有一个简单的代码:

    $input = '<p>ěščřžýáíé</p><p><img alt="" src="http://www.test.com/img.jpg" style="width: 100px; height: 100px;"></p>';
    $dom = new DOMDocument('1.0', 'UTF-8');
    $dom->encoding = 'UTF-8';
    $dom->loadHTML($input, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $imgs = $dom->getElementsByTagName('img');
    foreach($imgs as $img){
        $src = $img->getAttribute('src');
        $style = $img->getAttribute('style');
        $newSrc = 'http://www.test.com/img001.jpg';
        $img->setAttribute( 'src' , $newSrc );
    }
    $content = $dom->saveHTML();
    

    问题是输出是经过编码的。 我希望输入的字符与输入的字符相同。

    <p>&Auml;&#155;&Aring;&iexcl;&Auml;&#141;&Aring;&#153;&Aring;&frac34;&Atilde;&frac12;&Atilde;&iexcl;&Atilde;&shy;&Atilde;&copy;<p><img alt="" src="http://www.test.com/img001.jpg" style="width: 100px; height: 100px;"></p></p>
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Nigel Ren    7 年前

    saveHTML() utf8_decode() 结果。。。

    $content = utf8_decode($dom->saveHTML($dom->documentElement));
    

    给予。。。

    <p>ěščřžýáíé<p><img alt="" src="http://www.test.com/img001.jpg" style="width: 100px; height: 100px;"></p></p>
    
    推荐文章