user632347
user632347

Reputation: 835

Using preg_match to filter youtube URLS in php

I am using the following code to convert youtube URLS in my posts to embedded videos. But this is not working if I add the URL with https:// and youtu.be format. How should I fix it?

  $youtube_url = "/http:\/\/www\.youtube\.com\/watch\?(.*?)v=([a-zA-Z0-9_\-]+)(\S*)/i";
  if (preg_match($youtube_url, $text, $youtube)){
   // echo "match found";
  }

Upvotes: 0

Views: 1567

Answers (1)

null
null

Reputation: 11889

Regexp would look like /https?:\/\/(?:www\.)?youtu(?:\.be|be\.com)\/watch(?:\?(.*?)&|\?)v=([a-zA-Z0-9_\-]+)(\S*)/i

s? means optional s character. There is not really much else to comment in this regexp.

Upvotes: 3

Related Questions