Get source code with Chinese characters PHP

故事扮演 提交于 2019-12-23 20:22:27

问题


Well, I give up. I've been messing around with all I could think of to retrieve data from a target website that has information in traditional Chinese encoding (charset=GB2312).

I've been using the simple_html_parser like always but it doesn't seem to return the Chinese characters, in fact all I get are some weird question marks embedded inside a rhomboid shape. ("�������ѯ�ؼ��֣�" Like so)

Declaring the encoding for the php file didn't do anything except of getting rid of some unwanted character showing at the start of the page.

By declaring it I mean:

header('Content-Type', 'text/html; charset=GB2312');

I can't get any data that's written in Chinese, also tried file_get_contents with the same luck. I'm probably missing something obvious since I can't find any related discussion elsewhere.

Thanks in advance.


回答1:


Have you tried converting the encoding with mb_convert_encoding or iconv, e.g.

$str = mb_convert_encoding($content, 'UTF-8', 'GB2312');

or

$str = iconv("UTF-8", "GB2312//IGNORE", $content);



回答2:


Get it in whatever character set the source uses, then convert it to something usable locally, such as UTF-8. Then send it to the browser.




回答3:


set header('Content-Type: text/html; charset=utf-8');

It's working for me



来源:https://stackoverflow.com/questions/2248532/get-source-code-with-chinese-characters-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!