duanci6484 2013-05-08 09:18
浏览 49

PHP中的Twitter API字符编码问题

I'm trying to integrate Twitter API V1.1 with a PHP website. On Twitter my tweet appears as:

SWLaw : Farm & Country is out! http://paper.li/SWLAWCOUK/1346753760 … ▸ Top stories today via @CountrySportsSW @jonesFW @NFUBFGmag

However, on my site the tweet appears as:

RT @SWLawCountry: SWLaw : Farm & Country is out! http://t.co/AU7WCxUxf5â–¸ Top stories today vi@CountrySportsSWS@jonesFWF@NFUBFGmagag

Note the additional characters and special characters.

This seems to be a character encoding issue because the Twitter API (I believe) returns JSON encoded as UTF-8 whereas my web page is encoded as iso-8859-1. I've attempted to fix the issue using PHP's iconv function as follows:

$new_text = iconv("UTF-8", "ISO-8859-1//IGNORE", $new_text);

This produces the following output:

RT @SWLawCountry: SWLaw : Farm & Country is out! http://t.co/AU7WCxUxf5 Top stories today vi@CountrySportsSWS@jonesFWF@NFUBFGmagag

Note that although the special characters have been removed, the additional characters remain and there are missing spaces between some words.

Does anyone know how I can overcome this problem?

  • 写回答

1条回答 默认 最新

  • drvvepadi289421028 2013-05-08 09:28
    关注

    The best thing to do would be to use UTF-8 for your site. This is pretty much the de-facto standard on the web these days and can encode virtually any and all characters in use on computers to date.

    If it's difficult for you to switch over, you can represent characters that are not encodable in ISO-8859 by encoding them as HTML entities instead:

    echo htmlentities($new_text, ENT_NOQUOTES, 'UTF-8');
    

    This should preserve all characters, even in an ISO-8859 context.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向