I want to get the YouTube video ID from YouTube embed code using preg_match or regex. For a example
<iframe width="560" height="315" src="//www.youtube.com/embed/0gugBiEkLwU?rel=0" frameborder="0" allowfullscreen></iframe>
I want to take the ID 0gugBiEkLwU
Can anyone tell me how to do this. Really appropriate your help.
You can use :
Check Demo Here
Explanation :
The below function will extract the youtube video id from teh all format of youtube urls,
Using this pattern with a capturing group should give you the string you want:
example: https://regex101.com/r/kH5kA7/1
I know this is pretty late, but I came up with something for people who might still be looking. Since not all Youtube iframe src attributes end in "?rel=", and can sometimes end in another query string or end with a double quote, you can use:
This captures anything after "/embed/" and before the ending double-quote/query string. The selection can include any letter, number, underscore and hyphen.
Here's a demo with multiple examples: https://regex101.com/r/eW7rC1/1