Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Help with Regular Expressions php - preg_match

    From the code bellow i need to get only the youtube video id in this case ko6oQgodiV8 which is the unique key after youtube.com/v/ from this content:

    [AYoutube http://www.youtube.com/v/ko6oQgodiV8...youtube_gdata]

    I tried this but it didnt work

    Code:
    preg_match('/v/(.*?)[&\"]/i\]/',$content,$video_id);
    $thumb_url = 'http://img.youtube.com/vi/' . $video_id[1] . '/0.jpg';
    return 1;
    Last edited by jetonr; 01-21-2010 at 11:58 PM. Reason: Resolved

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Untested:

    Code:
    preg_match('#/v/(.+?)\?#', $content, $video_id);

  • Users who have thanked MattF for this post:

    jetonr (01-21-2010)

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Resolved thnx man, that was so easy for you :P but i lost 3 hours

  • #4
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Personally I would use
    /v/\K[^\?]+
    since it is possible the URL won't contain a ? after the video ID, so this will match just
    /v/ko6oQgodiV8

    Full code:
    PHP Code:
    preg_match('#/v/\K[^\?]+#',$content,$video_id);
    $thumb_url 'http://img.youtube.com/vi/' $video_id[0] . '/0.jpg'
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    There are always multiple answers for one question. This would also achieve the same:

    Code:
    preg_match('#/v/([\w\d]+)#i', $content, $video_id);
    thumb_url = 'http://img.youtube.com/vi/' . $video_id[1] . '/0.jpg';
    There'll be more ways than you can shake a stick at to achieve the result.
    Last edited by MattF; 01-22-2010 at 06:21 AM.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    indeed, although you don't need the \d since \d is part of \w
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by JAY6390 View Post
    indeed, although you don't need the \d since \d is part of \w
    Cheers for the pointer. As you can probably tell, I'm a tad on the rusty side with regex.

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    lol np. I don't understand why exactly they have _ in the \w character class. Makes no sense imo, and a pain in the *** if you want to use a-z0-9 you have to put all that. It would be better if you could just use [\w_] in a character class
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Tags for this Thread

    Posting Permissions

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