So, I'm writing the code that is supposed to parse different websites, and some of them use windows-1250
encoding, and some of them use 'utf-8'. I don't have any impact over those websites, and you can probably guess that those pages with 'windows-1250' are giving me headache. So, here's the code that I'm using:
$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();
and here's the output in browser when I try to run this script:
Warning: DOMDocument::saveHTML(): output conversion failed due to conv error, bytes 0x9A 0x61 0x72 0x6B
So, is there a way to handle this error with 'windows-1250' encoding, that will work work utf-8 also ? I tried using utf_encode
with $response
and that passes, but then international characters are messed up.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…