【发布时间】:2013-01-15 11:08:12
【问题描述】:
除了我之前关于parsing images and text from complex xml 的问题之外,现在唯一的问题是我没有得到正确的编码。文本为希腊语,xml 文件具有utf-8 编码。
这是解析xml的代码:
$xml = simplexml_load_file('myfile.xml');
$descriptions = $xml->xpath('//item/description');
foreach ( $descriptions as $description_node ) {
$description_dom = new DOMDocument();
$description_dom->loadHTML( (string)$description_node );
$description_sxml = simplexml_import_dom( $description_dom );
$imgs = $description_sxml->xpath('//img');
$text = $description_sxml->xpath('//div');
foreach($imgs as $image){
echo (string)$image['src'];
}
foreach($text as $t){
echo (string)$t;
}
}
如果我echo $description_node,文本看起来不错,但在我得到$description_dom 和simplexml_import_dom之后,它看起来像这样:
Ïε ιÏλαμικÎÏ ÎºÎ¿Î¹Î½ÏÏηÏεÏ.使用 mb_convert_encoding 将其变为:
ýÃÂñù" ÃÂ。我做错了什么?
【问题讨论】:
-
您是否尝试添加 from_encoding 参数?像
mb_convert_encoding($str, "UTF-7", "EUC-JP");也使用正确的编码DomDocument -
当您将某些字符串“回显”到浏览器时,请确保您从格式良好的 HTML 页面执行此操作,并指定了 UTF-8 字符集:
<!doctype html> <html> <head> <meta charset="utf-8" /> </head> <body> <?php echo $text; ?> </body> </html>这已经可以让您免于一些无用的头痛。 -
simplexml_load_file已经在 utf-8 中加载了所有内容,请尝试删除,'utf-8'额外的转换可能 -
@PeterM 是的,但是将它从 ..what 转换为 utf-8?
-
来自 myfile.xml 的@pano 编码。也许它是不同的编码?如果是这样,请尝试将文件 before 转换为
simplexml_load_file,在这种情况下应使用simplexml_load_string。也尝试在浏览器中显示原始 xml 文件,看看它是否正确呈现。
标签: php encoding utf-8 xml-parsing