Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-使用file_get_内容更改阿拉伯字符的字符集_Php_Utf 8_File Get Contents_Arabic - Fatal编程技术网

PHP-使用file_get_内容更改阿拉伯字符的字符集

PHP-使用file_get_内容更改阿拉伯字符的字符集,php,utf-8,file-get-contents,arabic,Php,Utf 8,File Get Contents,Arabic,我正在使用谷歌获得阿拉伯语翻译,这是我的代码: header('Content-Type: text/html; charset=UTF-8'); $page=file_get_contents("http://www.google.com/translate_t?langpair=en|ar&text=hello",FILE_TEXT); $page=substr($page,strpos($page,"TRANSLATED_TEXT")+strlen("TRANSLATED_TEXT

我正在使用谷歌获得阿拉伯语翻译,这是我的代码:

header('Content-Type: text/html; charset=UTF-8');
$page=file_get_contents("http://www.google.com/translate_t?langpair=en|ar&text=hello",FILE_TEXT);
$page=substr($page,strpos($page,"TRANSLATED_TEXT")+strlen("TRANSLATED_TEXT")+2);
$page=substr($page,0,strpos($page,"';INPUT_TOOL_PATH"));
echo mb_detect_encoding($page); // edited 2015/05/26
echo mb_convert_encoding($page, 'UTF-8', 'ISO-8859-6');
如果您按照file_get_contents函数上的链接进行操作,您将看到以下单词:محبا

但如果你运行代码,你会得到:كحبا

正如您所看到的,最后一个(或第一个)字符是不同的


我做错了什么?

将最后一行替换为:

echo iconv('WINDOWS-1256', 'UTF-8', $page);

我认为这是因为您使用了错误的编码,如果您检查页面返回的内容字符集元,您将看到它是windows-1256。

您可能不需要更改阿拉伯字符的字符集。您需要在显示字符的页面中设置正确的字符集/编码。使用UTF-8将起作用。这应该是有帮助的网页是谷歌,我不能修改它,我也认为文件内容使用他自己的字符集。顺便问一下,你为什么不使用谷歌翻译RESTAPI?这是一个付费服务,我只想翻译“你好:)你得到什么作为编码?如果使用:echo mb_detect_encoding($str)进行检查;