Skip to content
Advertisement

How to pregmatch youtube url with the youtube short url and get videoids from them php

Hi am trying to pregmatch youtube urls and youtube shorten url and get video ids from am able to pregmatch the youtube url but unable to pregmatch short url Here is my code..

 if (preg_match('@^(?:https://(?:www\.)?youtube.com/)(watch\?v=|v/)([a-zA-Z0-9_]*)@', $video_id, $match)) 

where videoid= https://www.youtube.com/watch?v=Xt4S-Ot0Jcs How can I pregmatch short url like https://youtu.be/Xt4S-Ot0Jcs how can I get the videoids from both using php

Advertisement

Answer

You can try with a simpler regex if you just want to get the videoId :

if (preg_match('@(youtube.com/watch?v=|youtu.be/)([a-zA-Z0-9_]*)@', $video_id, $match))
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement