...

View Full Version : Help with Regular Expressions php - preg_match



jetonr
01-21-2010, 11: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


preg_match('/v/(.*?)[&\"]/i\]/',$content,$video_id);
$thumb_url = 'http://img.youtube.com/vi/' . $video_id[1] . '/0.jpg';
return 1;

MattF
01-21-2010, 11:57 PM
Untested:



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

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

JAY6390
01-22-2010, 03:40 AM
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:

preg_match('#/v/\K[^\?]+#',$content,$video_id);
$thumb_url = 'http://img.youtube.com/vi/' . $video_id[0] . '/0.jpg';

MattF
01-22-2010, 06: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

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

MattF
01-22-2010, 04: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

JAY6390
01-22-2010, 04: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