Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm using the YouTube API to grab comments from videos in JSON, using the following code:

$url = 'https://gdata.youtube.com/feeds/api/videos/' . $video_id .'/comments?alt=json&max-results=50&v=2';
$comments = array();

$json   = file_get_contents($url);
$data   = json_decode($json, TRUE);

foreach($data["feed"]["entry"] as $item)
{
    array_push($comments, $item["content"]['$t']);
}

However there is some kind of character encoding problem as I keep getting '' in the comments - usually at the end of a sentence/comment.

Any ideas on how to read the JSON using the correct ASCII character encoding?

share|improve this question
 
In the JSON response from Google '\ufeff' is being inserted into the comments. Not sure if this is being interrupted as '' by json_decode... –  CDR LDN May 28 at 14:57
1  
add comment

1 Answer

up vote 0 down vote accepted

Thank you to Danack for pointing out that it is a Byte Order Mark (BOM).

This is also deals with the same problem - How do I remove  from the beginning of a file?

None of the solutions on there seemed to work, so I tackled it myself. Before going through json_decode, the special characters  were simply \ufeff so I simple removed them before decoding.

$temp   = file_get_contents($json_url, 0, null, null);
$temp   = str_replace('\ufeff', '', $temp);     
$data   = json_decode($temp, TRUE);
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.