0

I have tried this but its failing:

$whereIsMyCake = '';

//
// put all the links here
//
$url =  "http://www.youtube.com/embed/more_yougooglelink";

$url =  "http://www.youtube.com/v/..." // works
$url =  "http://www.youtube.com/watch?v=ue1mLnyEd90"; // WORKS
$url =  "http://youtu.be/we_youGoogle_u"; // WORKS


parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
if($my_array_of_vars['v']=='') { 
  preg_match(
    "#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#",
    $url, 
    $matches);

  $whereIsMyCake = $matches[0];
} else {
  $whereIsMyCake = $my_array_of_vars['v'];
}

echo $whereIsMyCake;

Question: Now youtube.com/embed/videoid is here seems not working, and what if youtube makes new links we have to keep changing our algorithms? for youtube.com/embed/embed.weareruling/embed/embed/googleisboss/findmenow/video id is here ?

Logical Fallacy
  • 3,017
  • 5
  • 25
  • 41
  • "Gimme teh codes"... What have you tried so far? Because simple `strstr` and `substr` should give you correct result pretty fast, what's the problem? – Vyktor Oct 08 '12 at 12:29

1 Answers1

1

You could use a more complete regex, as addressed in this answer: https://stackoverflow.com/a/6556662/921739. That answer also talks about YouTube's oembed service, which you might find useful.

Also, you could use the YouTube API https://developers.google.com/youtube/2.0/developers_guide_php. That way, if the format changes, you're still golden.

As a note to alleviate your concerns about YouTube's URLs changing: Think of all the blogs, websites, and emails that have links to YouTube videos. If the URL format was to change without support for the old format, all of those links would get broken. Google has good reason to change their URLs infrequently. (That's not to say they won't...)


EDIT

Using the code from the SO link I provided, the ID is pulled from the URL in all of the formats you need:

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;
}

$urls[] = "http://www.youtube.com/embed/ue1mLnyEd90";
$urls[] = "http://www.youtube.com/v/ue1mLnyEd90";
$urls[] = "http://www.youtube.com/watch?v=ue1mLnyEd90";
$urls[] = "http://youtu.be/ue1mLnyEd90";

foreach ($urls as $url) {
    assert(youtube_id_from_url($url) === 'ue1mLnyEd90'); # Passes
}
Community
  • 1
  • 1
Logical Fallacy
  • 3,017
  • 5
  • 25
  • 41
  • Thank you, i was confused which direction to take, i thought i would do it in python, but your logic is fine thank you. –  Oct 08 '12 at 14:02