I'm trying to validate YouTube URLs for my application.
So far I have the following:
// Set the youtube URL
$youtube_url = "www.youtube.com/watch?v=vpfzjcCzdtCk";
if (preg_match("/((http\:\/\/){0,}(www\.){0,}(youtube\.com){1} || (youtu\.be){1}(\/watch\?v\=[^\s]){1})/", $youtube_url) == 1)
{
echo "Valid";
else
{
echo "Invalid";
}
I wish to validate the following variations of Youtube Urls:
- With and without http://
- With and without www.
- With the URLs youtube.com and youtu.be
- Must have /watch?v=
- Must have the unique video string (In the example above "vpfzjcCzdtCk")
However, I don't think I've got my logic right, because for some reason it returns true for: www.youtube.co/watch?v=vpfzjcCzdtCk
(Notice I've written it incorrectly with .co
and not .com
)