Php DOMDocument与windows-1250编码

Php DOMDocument与windows-1250编码,php,encoding,utf-8,domdocument,Php,Encoding,Utf 8,Domdocument,因此,我正在编写解析不同网站的代码,其中一些使用windows-1250编码,一些使用“utf-8”。我对那些网站没有任何影响,你可能会猜到那些带有“windows-1250”的页面让我头疼。下面是我使用的代码: $doc = new DOMDocument(); @$doc->loadHTML($response); $xpath = new DOMXpath($doc); $anchors = $xpath->query(

因此,我正在编写解析不同网站的代码,其中一些使用
windows-1250
编码,一些使用“utf-8”。我对那些网站没有任何影响,你可能会猜到那些带有“windows-1250”的页面让我头疼。下面是我使用的代码:

    $doc = new DOMDocument();
        @$doc->loadHTML($response);

        $xpath = new DOMXpath($doc);
        $anchors = $xpath->query("//a[@href]");
        foreach( $anchors as $anchor) {
            $href = $anchor->getAttribute("href");
            $anchor->setAttribute("href", 'http://example.com/');
        }

        $response = $xpath->document->saveHTML();
下面是我尝试运行此脚本时在浏览器中的输出:

Warning: DOMDocument::saveHTML(): output conversion failed due to conv error, bytes 0x9A 0x61 0x72 0x6B

那么,有没有办法用“windows-1250”编码来处理这个错误,utf-8也能用?我尝试将
utf\u encode
$response
一起使用,但是国际字符被弄乱了。

如果您只是想更改所有锚定标记的href,那么您可以使用jquery

代码如下所示:

  //loop through the anchor tags
 $("a").each(function(){//begin each function

  //set the href attributes
  $(this).attr("href","http://example.com/");


  });//end each function
下面是一个JSFIDLE示例:


如果将鼠标悬停在链接上,您将看到它们已被更改。

您确定其
1250
?不是
1251/1252
?。不管怎样,你能重现这个问题吗?好的,这是直接从页面上看出来的。我不知道为什么会发生这种情况,因为有些页面使用这种编码工作得很好,在有些页面上,我得到了这个错误。你有一个可以检查的实时站点吗?这样我们就可以看到我现在看到的问题了。你为什么使用php是有道理的