title: RegEx pattern to get the YouTube video ID from any YouTube URL
body: <p>Let's take these URLs as an example:</p>
  
  <ol>
  <li><a href="http://www.youtube.com/watch?v=8GqqjVXhfMU&amp;feature=youtube_gdata_player">http://www.youtube.com/watch?v=8GqqjVXhfMU&amp;feature=youtube_gdata_player</a></li>
  <li><a href="http://www.youtube.com/watch?v=8GqqjVXhfMU">http://www.youtube.com/watch?v=8GqqjVXhfMU</a></li>
  </ol>
  
  <p>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.</p>
  
  <pre><code>/**
   * 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;
  }
  </code></pre>
  
  <p>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?</p>