Extract YouTube ID from Submitted URL

Written by on

I have a site where i need to allow people to upload a YouTube URL. I want to just store the ID in the MySQL DB and I need to be able to retrieve it later for embedding on the site. Sometimes people will paste a url cleanly like this...


..where you could just explode the values at the equals sign and get the second item in the array to parse out the YouTube ID. It is nice to just have the YouTube ID so that you can use it as you need it to retrieve any info about the video that you might need.

Sometimes they paste the url like this:


So i needed a way to extract the "v" variable out of this url no matter how it is submitted into the DB.

This code allows you to get the query string variable that you are seeking...independent upon how many variables exist.

Real Time Web Analytics ^