来自twitter API的JSON包含\ u2019

时间:2010-11-18 04:58:08

标签: php mysql json twitter

这是我通过twitter API获取的JSON文件的一部分:

  

“文字”:“科学家们发现了研究分子的新方法:女王研究人员已经发现了研究方法...... http://bit.ly/chbweE

我在项目中使用PHP。

使用json_decode函数后,\ u2019被转换为',这基本上很烦人。

我尝试使用$raw_json = preg_replace("u2019","'",$raw_json),但json_decode函数返回NULL。

还有其他方法可以将\ u2019转换为'?

由于


编辑:

这就是我获取JSON的方式:

// create curl resource
    $ch = curl_init();

    // set url
    curl_setopt($ch, CURLOPT_URL, "http://search.twitter.com/search.json?q=from%3Agizmodo&rpp=10");

    //return the transfer as a string
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    // $output contains the output string
    $raw_json = curl_exec($ch);

    // close curl resource to free up system resources
    curl_close($ch);

2 个答案:

答案 0 :(得分:6)

这可能是您在输出中使用的字符集的问题。如果您要输出到HTML页面,请尝试将以下元标记添加到您的头部:

<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

答案 1 :(得分:0)

您错过了/.../中的正则表达分隔符preg_replace。尝试:

$raw_json = preg_replace("/u2019/","'",$raw_json);

另外,为了更安全,我只会替换那些实际为转义字符的实例:

$raw_json = preg_replace("/\\\\u2019/","\\'",$raw_json);
相关问题