I’m working on a project and there is something I can’t quite solve.
Users can add YouTube videos to their profiles by just adding the YouTube clip’s link in a form, E.G:
Youtube link: youtube.com/watch?v=9YgjZ4oPrj4
Now once the user clicks submit the video has to be embedded. The only problem is that I only need this part 9YgjZ4oPrj4 for embedding
(see code below)
<iframe width="560" height="315" src="http://www.youtube.com/embed/9YgjZ4oPrj4" frameborder="0" allowfullscreen></iframe>
Now, as you know, YouTube videos have different types of links:
youtube.com/watch?v=9YgjZ4oPrj4
youtu.be/9YgjZ4oPrj4
How do I make this one work?
It’s actually the same as Facebook uses
Here’s some code that will pull the YouTube ID from a string. Note I just copied it from Forrst, but it should do what you want.
The following urls were tested.