PHP reads the records in the database to generate XML. how can this problem be solved when the records contain Chinese characters?
Source: Internet
Author: User
When PHP reads the records in the database to generate XML, the records contain Chinese characters. what should I do if I want to use the DOM function of PHP to generate XML documents? if the documents contain Chinese characters, an error occurs, that is, it is useless to use CDATA to contain Chinese characters, the code is as follows: & lt ;? & Nbsp; require ('Conn. php'); $ owen1 = 'Student Work'; header (what should I do if the record contains Chinese characters when 'co php reads the records in the database to generate XML?
I want to use the PHP DOM function to generate an XML document. if the document contains Chinese characters, an error occurs. that is, it is useless to use CDATA to contain Chinese characters. the code is as follows:
$ Owen1 = 'Student Work ';
Header ('content-type: text/XML ');
$ Result = $ conn-> query ("select * from news where BigClassName = '$ owen1 'limit 4 ");
If ($ result)
{
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
$ Dom-> formatOutput = true;
$ Rcs = $ dom-> createElement ("rss ");
}
Run the command, and you will be prompted to encounter unformatted characters in the latest news. how can I change it? share it:
------ Solution --------------------
Data to be written into XML using UTF-8 encoding
Two solutions:
1. The data read from the database in your code is transcoded using the iconv function.
$ Date = iconv ('gbk', utf-8 ', $ row ['title']);
$ Datetext = $ dom-> createTextNode (iconv ('gbk', utf-8 ','最新动态'));
Other similar
2. read UTF-8 encoded data directly from the database
$ Conn-> query ("set nemes utf8 ");
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.