PHP parses XML. First, let's talk about the encoding problem. if the encoding of the XML file and the page file is inconsistent, garbled code will be generated. Solve Chinese garbled problem can be output with the following statement: echoiconv (UTF-8, G first to mention the encoding problem, if the XML file and the page file encoding is inconsistent, then garbled generated. Echo iconv ("UTF-8", "GBK", $ Song_Url );
PHP webpage code
The php file encoding should match the webpage encoding. if you want to use gb2312 encoding, php should output the header: header ("Content-Type: text/html; charset = gb2312 "), add static pages The encoding format of all files is ANSI, which can be opened in Notepad. save it as ANSI and overwrite the source file.
If you want to use UTF-8 encoding, php needs to output headers: header ("Content-Type: text/html; charset = utf-8") and add static pages The encoding format of all files is UTF-8. It may be a little troublesome to save it as UTF-8. generally, BOM is generated at the beginning of the UTF-8 file. If session is used, problems may occur. you can use editplus to save it in editplus, tool-> parameter selection-> File-> UTF-8 signature, select always delete, and then save to remove the BOM information.
Php itself is not Unicode, and all functions such as substr must be changed to mb_substr (mbstring extensions need to be installed); or iconv transcoding is used.
PHP and Mysql data interaction PHP and database encoding should be consistent
Modify the mysql configuration file my. ini or my. cnf. mysql is better to use utf8 encoding [mysql]
Default-character-set = utf8 [mysqld] default-character-set = utf8default-storage-engine = MyISAM Add: default-collation = utf8_bininit_connect = 'set NAMES utf8' under [mysqld'
Add mysql_query ("set names 'code'") before the php program that requires database operations. the encoding is consistent with the php code. if the php code is gb2312, the mysql code is gb2312, if it is UTF-8, mysql encoding is utf8, so that no garbled characters will appear during data insertion or retrieval.
PHP related to the operating system
The encoding for Windows and Linux is different. in Windows, if the parameter is UTF-8 encoded when a function of PHP is called, an error occurs, such as move_uploaded_file (), filesize (), and readfile () these functions are often used for processing uploads and downloads. the following errors may occur during calls:
Warning: move_uploaded_file()[function.move-uploaded-file]:failed to open stream: Invalid argument in ...Warning: move_uploaded_file()[function.move-uploaded-file]:Unable to move '' to '' in ...Warning: filesize() [function.filesize]: stat failed for ... in ...Warning: readfile() [function.readfile]: failed to open stream: Invalid argument in ..
Although gb2312 encoding in Linux does not produce these errors, the stored file name becomes unreadable due to garbled characters. in this case, you can first convert the parameter to the encoding recognized by the operating system, encoding conversion can be performed using mb_convert_encoding (string, new encoding, original encoding) or iconv (original encoding, new encoding, string). In this way, the stored file name will not contain garbled characters, you can also normally read files to upload and download files with Chinese names.
In fact, there are still better solutions to completely break away from the system, so you don't have to consider the encoding of the system. You can generate a sequence with only letters and numbers as the file name, and store the original Chinese name in the database. in this way, calling move_uploaded_file () will not cause problems, during the download, you only need to change the file name to the original name with Chinese characters. The download code is as follows:
header("Pragma: public");header("Expires: 0");header("Cache-Component: must-revalidate, post-check=0, pre-check=0");header("Content-type: $file_type");header("Content-Length: $file_size");header("Content-Disposition: attachment; filename="$file_name"");header("Content-Transfer-Encoding: binary");readfile($file_path);
$ File_type is the file type, $ file_name is the original name, and $ file_path is the address of the file stored on the service.
Book. xml
Jack Herrington
PHP Hacks
O'Reilly
Jack Herrington
Podcasting Hacks
O'Reilly
Use the DOM library to read XML:
load( 'books.xml' );$books = $doc->getElementsByTagName( "book" );foreach( $books as $book ){$authors = $book->getElementsByTagName( "author" );$author = $authors->item(0)->nodeValue;$publishers = $book->getElementsByTagName( "publisher" );$publisher = $publishers->item(0)->nodeValue;$titles = $book->getElementsByTagName( "title" );$title = $titles->item(0)->nodeValue;echo "$title - $author - $publishern";}?>
Read XML with a SAX parser:
Parse XML using regular expressions:
(.*?)/s", $xml, $bookblocks );foreach( $bookblocks[1] as $block ){preg_match_all( "/(.*?)/", $block, $author );preg_match_all( "/(.*?)/", $block, $title );preg_match_all( "/
(.*?)
/", $block, $publisher );echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."n" );}?>
Bytes. To solve the Chinese garbled problem, use the following statement: echo iconv ("UTF-8", "G...