April 23, 2012

how to parse json in php and javascript

Question by Jetoox

http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5

I would like to extract the video thumbnail and video link using PHP and JavaScript from the url above but I’m not sure how to do it, here is my attempt so far:

$json = 'http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5';
$data = json_decode($json);
print_r($data);

Answer by Gäng Tian

in PHP

  $tmp = file_get_contents('http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5/');
  $data = json_decode($tmp,true);

in javascript with jquery

  $.get("http://gdata.youtube.com/feeds/users/LadyGagaVEVO/uploads?alt=json&max-results=10&format=5/", function(response) { 
       var data = $.parseJSON(response);
  });

about the illegal character, depends on what counts as illegal in your case, just do some string validation as you traverse through the object for the thumbnail and video link you looking for.

Answer by Starx

Using, PHP as you are doing, use json_decode(). but use it one extra paramter

$data = json_decode($string, true); // the true in the last will change into associative array

For JavaScript, as Kolink said, use JSON.parse()

March 7, 2012

RegEx pattern to get the YouTube video ID from any YouTube URL

Question by Shackrock

Let’s take these URLs as an example:

  1. http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player
  2. http://www.youtube.com/watch?v=8GqqjVXhfMU

This PHP function will NOT properly obtain the ID in case 1, but will in case 2. Case 1 is very common, where ANYTHING can come behind the YouTube ID.

/**
 * get YouTube video ID from URL
 *
 * @param string $url
 * @return string YouTube video id or FALSE if none found. 
 */
function youtube_id_from_url($url) {
    $pattern = 
        '%^# Match any YouTube URL
        (?:https?://)?  # Optional scheme. Either http or https
        (?:www.)?      # Optional www subdomain
        (?:             # Group host alternatives
          youtu.be/    # Either youtu.be,
        | youtube.com  # or youtube.com
          (?:           # Group path alternatives
            /embed/     # Either /embed/
          | /v/         # or /v/
          | /watch?v=  # or /watch?v=
          )             # End path alternatives.
        )               # End host alternatives.
        ([w-]{10,12})  # Allow 10-12 for 11 char YouTube id.
        $%x'
        ;
    $result = preg_match($pattern, $url, $matches);
    if (false !== $result) {
        return $matches[1];
    }
    return false;
}

What I’m thinking is that there must be a way where I can just look for the “v=”, no matter where it lies in the URL, and take the characters after that. In this manner, no complex RegEx will be needed. Is this off base? Any ideas for starting points?

Answer by Starx

Instead of regex. I hightly recommend parse_url() and parse_str():

$url = "http://www.youtube.com/watch?v=8GqqjVXhfMU&feature=youtube_gdata_player";
parse_str(parse_url( $url, PHP_URL_QUERY ), $vars );
echo $vars['v'];    

Done

...

Please fill the form - I will response as fast as I can!