Reputation: 29
var_dump(preg_match("/^((http\:\/\/){0,}(www\.){0,}(youtube\.com){1}|(youtu\.be){1}(\/watch\?v\=[^\s]){1})$/", "http://www.youtube.com/watch?v=M7FIvfx5J10"));
This code will return 0 as it failed.. why? What is wrong in regex? You can test it here http://writecodeonline.com/php/
Upvotes: 0
Views: 787
Reputation: 316
Try this :
var_dump(preg_match("/^((http\:\/\/){0,}(www\.){0,}((youtube\.com){1}|(youtu\.be){1})\/watch\?v\=[^\s]*)$/", "http://www.youtube.com/watch?v=M7FIvfx5J10"));
It seems to work. You forgot the quantifier for the video id and parenthesis around the OR for youtube.com / youtu.be
Upvotes: 1