简体   繁体   中英

changing xml document lable (utf encoding) in php

I have a strange problem. I am trying to load some xml document using DomDocumnet function but it throws a strange warning saying that :

Warning: DOMDocument::load(): Document labelled UTF-16 but has UTF-8 content

There is no other errors or warnings etc. when I manually edit the xml file from

<?xml version="1.0" encoding="utf-16"?> to

<?xml version="1.0" encoding="utf-8"?>

it process it flawlessly .

I am looking for a solution by which i can just edit xml file in header before further processing.

Thanks in advance..!

not the absolute solution but a hack to solve this problem in such situations.

just replace the utf 16 to utf8 after loading the xml document as string and before further processing by using preg_replace function.

$source_xml_file = file_get_contents($incorrect_xml_file, TRUE);
$corrected_xml_file= preg_replace('/(<\?xml[^?]+?)utf-16/i', '$1utf-8', $source_xml_file);
$xmldoc->loadXML($corrected_xml_file);

works in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM