- $this->encoding = $matches[1];
- } else {
- $this->encoding = mb_detect_encoding($buf);
- }
-
- // Normalize to UTF-8 / ASCII
- if (! in_array(strtolower($this->encoding), array('us-ascii', 'iso-8859-1', 'utf-8'))) {
- $buf = mb_convert_encoding($buf, 'utf-8', $this->encoding);
- $this->encoding = 'utf-8';
+ $encoding = $matches[1];
+ if (strtoupper($encoding) !== $utf8) {
+ // xml_parse() fails on non UTF-8 encoding attr in XML decLaration
+ $buf = preg_replace('/<\?xml ([^>]*)\bencoding="[a-z0-9-_]+"/i', '<?xml $1', $buf);
+ // xml_parse() requires UTF-8 compatible encoding
+ $buf = mb_convert_encoding($buf, $utf8, $encoding);
+ }