Mihai
Mihai

Reputation: 29

preg_match regex part fails for youtube urls

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

Answers (1)

Julien Rouvier
Julien Rouvier

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

Related Questions