Wakenn
Wakenn

Reputation: 391

Get YouTube ID from URL regex pattern

I've seen a couple different examples on the site but it doesn't get the id out of all the youtube options... as an example the following links don't work with the regex pattern below. any help would be wonderful. Thanks in advance:

It just seems to be this one if a user goes to youtube homepage and clicks on one of the vids there they give this url: http://www.youtube.com/watch?v=hLSoU53DXK8&feature=g-vrec

my regex puts it in the database as: hLSoU53DXK8-vrec and i need it without -vrec.

// YOUTUBE
$youtube = $_POST['youtube'];


 function getYoutubeId($youtube) {
    $url = parse_url($youtube);
    if($url['host'] !== 'youtube.com' &&
             $url['host'] !== 'www.youtube.com'&&
             $url['host'] !== 'youtu.be'&&
             $url['host'] !== 'www.youtu.be')
        return false;
    $youtube = preg_replace('~
        # Match non-linked youtube URL in the wild. (Rev:20111012)
        https?://         # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)? # Optional subdomain.
        (?:               # Group host alternatives.
          youtu\.be/      # Either youtu.be,
        | youtube\.com    # or youtube.com followed by
          \S*             # Allow anything up to VIDEO_ID,
          [^\w\-\s]       # but char before ID is non-ID char.
        )                 # End host alternatives.
        ([\w\-]{11})      # $1: VIDEO_ID is exactly 11 chars.
        (?=[^\w\-]|$)     # Assert next char is non-ID or EOS.
        (?!               # Assert URL is not pre-linked.
          [?=&+%\w]*      # Allow URL (query) remainder.
          (?:             # Group pre-linked alternatives.
            [\'"][^<>]*>  # Either inside a start tag,
          | </a>          # or inside <a> element text contents.
          )               # End recognized pre-linked alts.
        )                 # End negative lookahead assertion.
        [?=&+%\w]*        # Consume any URL (query) remainder.
        ~ix', 
        '$1',
        $youtube);
    return $youtube;
}

$youtube_id = getYoutubeId($youtube);

Upvotes: 0

Views: 6027

Answers (3)

Wakenn
Wakenn

Reputation: 391

$youtube = "theURL";
$query_string = array();

parse_str(parse_url($youtube, PHP_URL_QUERY), $query_string);

$youtube_id = $query_string["v"];

Upvotes: 0

Shannon Antonio Black
Shannon Antonio Black

Reputation: 111

Unfortuneately the solution above does not retrieve the Youtube ID for the short url "http://youtu.be". So based on the solutions above I wrote this function:

function get_youtube_id( $youtube_url ) {
    $url = parse_url($youtube_url);

    if( $url['host'] !== 'youtube.com' &&
        $url['host'] !== 'www.youtube.com'&&
        $url['host'] !== 'youtu.be'&&
        $url['host'] !== 'www.youtu.be')
    return '';

    if( $url['host'] === 'youtube.com' || $url['host'] === 'www.youtube.com' ) :
        parse_str(parse_url($youtube_url, PHP_URL_QUERY), $query_string);
        return $query_string["v"];
    endif;

    $youtube_id = substr( $url['path'], 1 );
    if( strpos( $youtube_id, '/' ) )
        $youtube_id = substr( $youtube_id, 0, strpos( $youtube_id, '/' ) );

    return $youtube_id;

}

Upvotes: 3

Ben
Ben

Reputation: 16553

$url = "http://www.youtube.com/watch?v=hLSoU53DXK8&feature=g-vrec";
$query_string = array();

parse_str(parse_url($url, PHP_URL_QUERY), $query_string);

$id = $query_string["v"];

Upvotes: 14

Related Questions