SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    how to get youtube video code from a URL

    hello guys,
    I'm using youtube API to post videos to my website, this API needs only the vid ID which is to be posted.
    Can anyone tell me how can i get a video ID from a youtube URL?

    for example from these URLs i want only the red part:
    youtube.com/watch?v=KQ6zr6kCPj8&ob=av3e
    youtube.com/watch?v=pIOOwhmkoLo&feature=player_embedded

    urls can contain different parameters but i want only the string between the "v=" till it meets an "&" or end of the string.

    Thanks.

  2. #2
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
        $str 'youtube.com/watch?v=KQ6zr6kCPj8&ob=av3e';
        
        
    preg_match('~v=([A-Za-z0-9]+)~'$str$match);
        
        
    print_r($match); 

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey, thanks for the answer however isnt there any simpler way that works like i said? anything between "v=" and "&" or end of string?
    I say it because that string may contain about anything, not only letters/numbers, an example of link is this:
    youtube.com/watch?v=-IqfDiA-esA

    So if its possible to do it the way i want it'll be better, if not then i can use the regex u gave while just adding '-' and hope no other special chars will pop-up

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ulthane View Post
    Hey, thanks for the answer however isnt there any simpler way that works like i said? anything between "v=" and "&" or end of string?
    I say it because that string may contain about anything, not only letters/numbers, an example of link is this:
    youtube.com/watch?v=-IqfDiA-esA

    So if its possible to do it the way i want it'll be better, if not then i can use the regex u gave while just adding '-' and hope no other special chars will pop-up
    You can use a dot like this to match any character

    PHP Code:
    preg_match('~v=(.*?)&~'$str$match); 

  5. #5
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    hey thanks, however i tested it and it didn't work on the last example i gave when there's no amp (&) in the string at all, any solution?

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this
    Code:
    $str = 'youtube.com/watch?v=-IqfDiA-esA';
    $pattern = '#v=([^&]+)#i';
    if (preg_match($pattern, $str, $match))
    {
            $id = $match[1];
            echo $id;
    }

  7. #7
    SitePoint Evangelist
    Join Date
    Jun 2010
    Location
    Israel
    Posts
    523
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    works in all situations, thank you both for the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •