Can someone show me how to get the youtube id out of a url regardless of what other GET variables are in the URL.
Use this video for example: http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related
So between v=
and before the next &
Can someone show me how to get the youtube id out of a url regardless of what other GET variables are in the URL.
Use this video for example: http://www.youtube.com/watch?v=C4kxS1ksqtw&feature=related
So between v=
and before the next &
This can be very easily accomplished using parse_str and parse_url and is more reliable in my opinion.
My function supports the following urls:
Also includes the test below the function.
We know that video ID is 11 chars length and can be preceded by
v=
orvi=
orv/
orvi/
oryoutu.be/
. So the simplest way to do this:And output:
fixed based on How to validate youtube video ids?
I had some post content I had to cipher throughout to get the Youtube ID out of. It happened to be in the form of the
<iframe>
embed code Youtube provides.The following pattern I got from @rob above. The snippet does a
foreach
loop once the matches are found, and for a added bonus I linked it to the preview image found on Youtube. It could potentially match more types of Youtube embed types and urls:Rob's profile: https://stackoverflow.com/users/149615/rob
This should do it as well.
I know that the title of the thread refers to the use of a regex, but just as the Zawinski quote says, I really think that avoiding regexes is best here. I'd recommend this function instead:
I recommend this because the ID of YouTube videos is always the same, independent from the style of the URL, e.g.
http://www.youtube.com/watch?v=t_uW44Bsezg
http://www.youtube.com/watch?feature=endscreen&v=Id3xG4xnOfA&NR=1
and that might be the case for embedded and
iframe
-ed stuff.