PHP - 如何使用阿拉伯字符解析XML

时间:2018-02-05 21:21:43

标签: php xml character-encoding

我正面临PHP中的解析问题。事实上,当我用法语解析谷歌建议结果时,我试图解析的网址是:

http://suggestqueries.google.com/complete/search?client=toolbar&hl=fr&q=exemple

我用以下代码解析它:

$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=fr&q=exemple';
$xml =simplexml_load_string(utf8_encode(file_get_contents(urldecode($file ))));
print_r($xml);

结果是oK。

当我用阿拉伯语做的时候,我替换lang和查询。代码如下

$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=من هو';
$xml =simplexml_load_string(utf8_encode(file_get_contents(urldecode($file ))));
print_r($xml);

网址没问题但我在解析时会收到警告

  

警告:   的file_get_contents(http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=من   هو):无法打开流:HTTP请求失败! HTTP / 1.0 400糟糕   请求

感谢shukshin.ivan解决了这个问题,但我现在正面临一个显示问题。 print_r返回如下

  

SimpleXMLElement对象([CompleteSuggestion] =>数组([0] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæÇÑØÛÑá)))[1] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãæåØØÑÞÑãÖÇä)))[2] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæãããããÏÏÇÇ))))[3] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæÈÇÈÇäæíá)))[4] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæãÓÓÌæ)))[5] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåååããããæÊ)))[6] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãæåæÐæÇáÞÑäää)))[7] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæÇææÚáíÇáÍÇßã)))[8] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæÚãÑæÈäÚËãÇä)))[9] =>   SimpleXMLElement对象([suggestion] => SimpleXMLElement对象(   [@attributes] =>数组([data] =>ãäåæÇææ())))))

编码是错误的吗?该页面的元字符集是utf8。 当我用硬编码的xml文件做同样的事情时,一切都很好。 我错过了什么吗?

如果你是俄罗斯人,我对西里尔字符有同样的问题

$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ru&q=' . urlencode('Я');

由于

1 个答案:

答案 0 :(得分:0)

你不应该urldecode整个网址。你应该urlencode(编码,而不是解码)字符串。以下代码工作正常。

$file = 'http://suggestqueries.google.com/complete/search?client=toolbar&hl=ar&q=' . urlencode('من هو');
$xml =simplexml_load_string(utf8_encode(file_get_contents($file )));
print_r($xml);