/** * Adjust xml string to the proper format * @param string $string - XML string document * @return string - Return the string converted */ public static function fixXmlHeader($string) { $string = XmlUtil::removeBom($string); if (strpos($string, "<?xml") !== false) { $xmltagend = strpos($string, "?>"); if ($xmltagend !== false) { $xmltagend += 2; $xmlheader = substr($string, 0, $xmltagend); if ($xmlheader == "<?xml?>") { $xmlheader = "<?xml ?>"; } } else { throw new XmlUtilException("XML header bad formatted.", 251); } // Complete header elements $count = 0; $xmlheader = preg_replace("/version=([\"'][\\w\\d\\-\\.]+[\"'])/", "version=\"" . self::XML_VERSION . "\"", $xmlheader, 1, $count); if ($count == 0) { $xmlheader = substr($xmlheader, 0, 6) . "version=\"" . self::XML_VERSION . "\" " . substr($xmlheader, 6); } $count = 0; $xmlheader = preg_replace("/encoding=([\"'][\\w\\d\\-\\.]+[\"'])/", "encoding=\"" . self::XML_ENCODING . "\"", $xmlheader, 1, $count); if ($count == 0) { $xmlheader = substr($xmlheader, 0, 6) . "encoding=\"" . self::XML_ENCODING . "\" " . substr($xmlheader, 6); } // Fix header position (first version, after encoding) $xmlheader = preg_replace("/<\\?([\\w\\W]*)\\s+(encoding=([\"'][\\w\\d\\-\\.]+[\"']))\\s+(version=([\"'][\\w\\d\\-\\.]+[\"']))\\s*\\?>/", "<?\\1 \\4 \\2?>", $xmlheader, 1, $count); return $xmlheader . substr($string, $xmltagend); } else { $xmlheader = '<?xml version="' . self::XML_VERSION . '" encoding="' . self::XML_ENCODING . '"?>'; return $xmlheader . $string; } }