php 读取xml的方法 (iconv解决中文乱码问题)

来源:互联网 发布:ppt图表数据不能修改 编辑:程序博客网 时间:2024/05/17 11:57

输出时用以下语句:echo iconv("UTF-8","GBK",$Song_Url); //解决中文乱码问题

一.首先是PHP网页的编码

1. php文件本身的编码与网页的编码应匹配

a.     如果欲使用gb2312编码,那么php要输出头:header(“Content-Type: text/html; charset=gb2312"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=gb2312">,所有文件的编码格式为ANSI,可用记事本打开,另存为选择编码为ANSI,覆盖源文件。

b.     如果欲使用utf-8编码,那么php要输出头:header(“Content-Type: text/html; charset=utf-8"),静态页面添加<meta http-equiv="Content-Type" content="text/html; charset=utf-8">,所有文件的编码格式为utf-8。保存为utf-8可能会有点麻烦,一般utf-8文件开头会有BOM,如果使用 session就会出问题,可用editplus来保存,在editplus中,工具->参数选择->文件->UTF-8签名,选择总 是删除,再保存就可以去掉BOM信息了。


2. php本身不是Unicode的,所有substr之类的函数得改成mb_substr(需要装mbstring扩展);或者用iconv转码。


二.PHP与Mysql的数据交互PHP与数据库的编码应一致

1.     修改mysql配置文件my.ini或my.cnf,mysql最好用utf8编码[mysql]
default-character-set=utf8
[mysqld]
default-character-set=utf8
default-storage-engine=MyISAM
在[mysqld]下加入:
default-collation=utf8_bin
init_connect='SET NAMES utf8'


2.     在需要做数据库操作的php程序前加mysql_query("set names '编码'");,编码和php编码一致,如果php编码是gb2312那mysql编码就是gb2312,如果是utf-8那mysql编码就是utf8,这样插入或检索数据时就不会出现乱码了。


三. PHP与操作系统相关

Windows和Linux的编码是不一样的,在Windows环境下,调用PHP的函数时参数如果是utf-8编码会出现错误,比如 move_uploaded_file()、filesize()、readfile()等,这些函数在处理上传、下载时经常会用到,调用时可能会出现下 面的错误:

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 ..


在Linux环境下用gb2312编码虽然不会出现这些错误,但保存后的文件名出现乱码导致无法读取文件,这时可先将参数转换成操作系统识别的编码,编码 转换可用mb_convert_encoding(字符串,新编码,原编码)或iconv(原编码,新编码,字符串),这样处理后保存的文件名就不会出现 乱码,也可以正常读取文件,实现中文名称文件的上传、下载。

其实还有更好的解决方法,彻底与系统脱离,也就不用考虑系统是何编码。可以生成一个只有字母和数字的序列作为文件名,而将原来带有中文的名字保存在数据库 中,这样调用move_uploaded_file()就不会出现问题,下载的时候只需将文件名改为原来带有中文的名字。实现下载的代码如下:

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是文件的类型,$file_name是原来的名字,$file_path是保存在服务上文件的地址。


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>


使用 DOM 库读取 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 - $publisher\n";
}
?>


用 SAX 解析器读取 XML

<?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";
}
?>

用正则表达式解析 XML

<?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" );
}
?>
0 0