PHP parsing XML Methods

Source: Internet
Author: User
Tags mysql code readfile

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. 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 <meta http-equiv = "Content-Type" content = "text/html; charset = gb2312"> to the static page. All files are encoded in ANSI format and can be opened in notepad, to overwrite the source file.

If you want to use UTF-8 encoding, php needs to output the header: header ("Content-Type: text/html; charset = UTF-8 "), add <meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"> to the static page. 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

<books><book><author>Jack Herrington</author><title>PHP Hacks</title><publisher>O'Reilly</publisher></book><book><author>Jack Herrington</author><title>Podcasting Hacks</title><publisher>O'Reilly</publisher></book></books>

Use the DOM library to read XML:

<?php$doc = new DOMDocument();$doc->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:

<?php$g_books = array();$g_elem = null;function startElement( $parser, $name, $attrs ) {global $g_books, $g_elem;if ( $name == 'BOOK' ) $g_books []= array();$g_elem = $name;}function endElement( $parser, $name ) {global $g_elem;$g_elem = null;}function textData( $parser, $text ){global $g_books, $g_elem;if ( $g_elem == 'AUTHOR' ||$g_elem == 'PUBLISHER' ||$g_elem == 'TITLE' ){$g_books[ count( $g_books ) - 1 ][ $g_elem ] = $text;}}$parser = xml_parser_create();xml_set_element_handler( $parser, "startElement", "endElement" );xml_set_character_data_handler( $parser, "textData" );$f = fopen( 'books.xml', 'r' );while( $data = fread( $f, 4096 ) ){xml_parse( $parser, $data );}xml_parser_free( $parser );foreach( $g_books as $book ){echo $book['TITLE']." - ".$book['AUTHOR']." - ";echo $book['PUBLISHER']."n";}?>

Parse XML using regular expressions:

<?php$xml = "";$f = fopen( 'books.xml', 'r' );while( $data = fread( $f, 4096 ) ) { $xml .= $data; }fclose( $f );preg_match_all( "/<book>(.*?)</book>/s", $xml, $bookblocks );foreach( $bookblocks[1] as $block ){preg_match_all( "/<author>(.*?)</author>/", $block, $author );preg_match_all( "/<title>(.*?)</title>/", $block, $title );preg_match_all( "/<publisher>(.*?)</publisher>/", $block, $publisher );echo( $title[1][0]." - ".$author[1][0]." - ".$publisher[1][0]."n" );}?>

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.