bammab
bammab

Reputation: 2563

How to match 2 urls with one regex pattern

I need help with a regex pattern to match 2 urls? The code I currently have matches the 1st url, but how would I make one pattern to match both urls. I am trying to obtain the random characters at the very end of each url.

http://www.videozer.com/video/yKKd

http://www.videozer.com/watch_video.php?v=rbgsd

preg_match('/http\:\/\/((www\.)?)videozer\.com\/video\/([a-zA-Z0-9]+)/', $URL, $result);

Upvotes: 1

Views: 2271

Answers (3)

Frank Farmer
Frank Farmer

Reputation: 39356

preg_match('@http://(?:www\.)?videozer\.com/(?:video/|watch_video\.php\?v=)([a-zA-Z0-9]+)@', $URL, $result);
  1. Use (abc|def) to match either the string 'abc' or 'def'
  2. Use a different delimiter so you don't have to escape /

Upvotes: 3

Nightfirecat
Nightfirecat

Reputation: 11610

You would just use matching groups. You can see the result here.

preg_match('/http\:\/\/((www\.)?)videozer\.com\/(video\/|watch_video\.php\?v=)([a-zA-Z0-9]+)/', $URL, $result);

Upvotes: 0

Rsaesha
Rsaesha

Reputation: 1114

This should do it:

preg_match('/http\:\/\/((www\.)?)videozer\.com\/(video\/|watch_video\.php\?v=)([a-zA-Z0-9]+)/', $URL, $result);

Upvotes: 1

Related Questions