1$url = "www.youtube.com/cCnrX1w5luM";
2
3$regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/";
4
5$match;
6
7if (
8 filter_var($url, FILTER_VALIDATE_URL)
9 && preg_match($regex_pattern, $url, $match)
10 ) {
11 echo "Youtube video id is: ".$match[4];
12} else {
13 echo "Sorry, not a youtube URL";
14}
15