开发者

JSON from twitter API contains \u2019

This is part of my JSON file I obtain through the twitter API:

"text":"Scientists discover new method for studying molecules: Queen\u2019s researchers have discovered the method for studyi... http://bit.ly/chbweE"

I am using PHP for my project.

After using the json_decode function, \u2019 is converted into ’, which basically is really annoying.

I tried using $raw_json = preg_replace("u2019","'",$raw_json), but then the json_decode function returns NULL.

Is there any other way I can convert the \u2019 into ' ?

Thanks


Edit:

This is how I am obtaining the 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);


This is probably a problem with the charset you're using in your output. If you're outputing to an HTML page, try adding the following meta tag to your head section:

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


You're missing the regex delimiters /.../ in preg_replace. Try:

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

Also, to be even safer, I'd only replace those instances that are actually escape characters:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜