I had a very similar problem and it was solved as follows. The main idea was to load the file into a string, replace all the bad objects with something like "[[entity]] Oslash;" and do a reverse swap before displaying some xml node.
function readXML($filename){ $xml_string = implode("", file($filename)); $xml_string = str_replace("&", "[[entity]]", $xml_string); return simplexml_load_string($xml_string); } function xml2str($xml){ $str = str_replace("[[entity]]", "&", (string)$xml); $str = iconv("UTF-8", "WINDOWS-1251", $str); return $str; } $xml = readXML($filename); echo xml2str($xml->forenames);
iconv ("UTF-8", "WINDOWS-1251", $ str), because my page has the encoding "WINDOWS-1251"
Krivoi
source share