View Full Version : Help with Regular Expressions php - preg_match

01-21-2010, 10:54 PM
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?f=videos&app=youtube_gdata]

I tried this but it didnt work

$thumb_url = 'http://img.youtube.com/vi/' . $video_id[1] . '/0.jpg';
return 1;

01-21-2010, 10:57 PM

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

01-21-2010, 10:59 PM
Resolved thnx man, that was so easy for you :P but i lost 3 hours :)

01-22-2010, 02:40 AM
Personally I would use
since it is possible the URL won't contain a ? after the video ID, so this will match just

Full code:

$thumb_url = 'http://img.youtube.com/vi/' . $video_id[0] . '/0.jpg';

01-22-2010, 05:18 AM
There are always multiple answers for one question. :D This would also achieve the same:

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. :D

01-22-2010, 01:10 PM
indeed, although you don't need the \d since \d is part of \w :)

01-22-2010, 03:19 PM
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. :D

01-22-2010, 03:31 PM
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

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum