ulthane — 2012-03-11T13:46:21-04:00 — #1
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:
urls can contain different parameters but i want only the string between the "v=" till it meets an "&" or end of the string.
wonshikee — 2012-03-11T14:55:46-04:00 — #2
$str = 'youtube.com/watch?v=KQ6zr6kCPj8&ob=av3e';
preg_match('~v=([A-Za-z0-9]+)~', $str, $match);
ulthane — 2012-03-11T16:00:26-04:00 — #3
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:
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
wonshikee — 2012-03-11T16:08:12-04:00 — #4
You can use a dot like this to match any character
preg_match('~v=(.*?)&~', $str, $match);
ulthane — 2012-03-11T16:32:57-04:00 — #5
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?
gvre — 2012-03-11T17:58:29-04:00 — #6
$str = 'youtube.com/watch?v=-IqfDiA-esA';
$pattern = '#v=([^&]+)#i';
if (preg_match($pattern, $str, $match))
$id = $match;
ulthane — 2012-03-11T18:23:18-04:00 — #7
works in all situations, thank you both for the help.