2

Here are examples.

www.youtube.com,
http://www.youtube.com/,
http://www.youtube.com/watch?v=YoB8t0B4jx4

I don't care if it has video or just youtube url but just valid url. What I want to do is when i know that it is a valid youtube url i will check if it has video ID. If it has ID i will add iframe for video and if has no it will be just plane text. I just want to know if a link is a valid youtube weather it has http or not, weather it has ID or not. Im not good at regular expression now and i need the code now pls help thanks in advance

white-comet
  • 381
  • 2
  • 4
  • 13

2 Answers2

8

How about this:

<?php
$url = "www.youtube.com/cCnrX1w5luM";

$regex_pattern = "/(youtube.com|youtu.be)\/(watch)?(\?v=)?(\S+)?/";
$match;

if(preg_match($regex_pattern, $url, $match)){
    echo "Youtube video id is: ".$match[4];
}else{
    echo "Sorry, not a youtube URL";
}

Demo

Look at the stdout section below in the demo

Krimson
  • 7,386
  • 11
  • 60
  • 97
0

If I understand your question correctly then it should be simple as youtube\.com/watch\?v=(.*)

Steve
  • 11,696
  • 7
  • 43
  • 81