PHP-generated XML is obtained in FLASH as garbled code.

Source: Internet
Author: User
You are not afraid to fight against incredible problems. I haven't encountered any obstacles in coding for a long time. today I have encountered another small tea bag, the XML generated by PHP, which is garbled by FLASH. It is finally solved through exploration. By the way, general solutions are also recorded. If you also encounter XML <-> FLASH garbled characters, you can quickly query:

1. make sure that XML is absolutely correct:
First, garbled characters occur when flash reads xml, involving the static attribute "System. useCodepage ".
Official description: "A Boolean value that tells Flash Player which code page to use to interpret external text files." The default value is false.
If we use an external text file encoded with UTF8, PHP100.com does not care about it. if we use a non-UTF8 text file that contains Chinese characters, you must set it to true to read the characters in flash without garbled characters.
In AS3, you can first import flash. system. System; then set System. useCodePage = true;

2. expect PHP to generate UTF-8-encoded XML:
To have Chinese characters in XML, you 'd better use UTF-8 encoding. Before using DOM to create XML, declare $ dom_XML = new DomDocument ('1. 0', 'utf-8'). The second parameter corresponds to the encoding value in the declaration part of the XML document. But note: this is only the declared encoding, and finally $ dom_XML-> saveXML () is used. the actual generated xml file is in the same format as the PHP script source file encoding, that is, "what encoding is your PHP and what encoding is the generated file."

3. XML file declaration encoding for UTF-8, flash no matter what useCodepage set value read are still garbled
This often happens because, although the declaration part is a UTF-8, the file itself is not a UTF-8 (I am an ANSI-encoded, sweaty ).
This will lead to a serious problem: Firefox can properly interpret XML, But IE (including IE kernel browsers such as TT) prompts that there are incorrect characters, and Flash is also garbled!
There are two solutions to this situation:

Change the XML declaration part to a non-UTF-8, such as GB2312, and then set the FLASH useCodepage = true;
Open the XML in Notepad and use the save as UTF-8 command.
That is to say, make the encoding declaration match the encoding of the file itself, and the name cannot be invalid.

4. PHP source files are not UTF-8 encoded. how can we generate XML that allows FLASH to support Chinese characters? That is, the situation I encountered:
PHP is saved by ANSI, and the XML generated by using DOM is also ANSI. if the XML file encoded by PHP100.com contains Chinese characters, it cannot be correctly read even if it is declared encoded as a UTF-8.
On this premise, to read XML correctly by FLASH without garbled characters, you must set encoding to GB2312.
Php dom cannot use GB2312 to write Chinese characters (puzzled, please give me some advice )? If new DomDocument ('1. 0', 'gb2312'); an error occurs when saving the XML: "output conversion failed due to conv error, bytes 0xCE 0xD2 0 x 5D 0 x 5D. That is to say, my ansi php can only generate the XML file declared as UTF-8 and actually ANSI, this file of course FLASH read out garbled (refer to 3rd ).

My solution:
After PHP generates XML with the UTF-8 declaration, a step is added: Open the XML file rewrite header declaration to replace the UTF-8 with GB2312. Hey, FLASH, IE, and FF!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.