Vignesh Chinnaiyan Vignesh Chinnaiyan - 1 year ago 477
Javascript Question

Regex pattern to extract domain name, video url and video id from a string containing youtube/vimeo url with PHP

I need a regex or php preg_match function that should extract youtube/vimeo url and the video provider/domain name like (vimeo/youtube) from a string containing a video url.

And from the extracted video url of string, i need to find the exact video id.

The regex should graph Video ID from the below url also,

  1. Youtube

  2. vimeo

Thanks, i am working on the solution. I will post as an answer if i find it.

Answer Source
$sample_text = "Cieker is the largest talentize social and professional networking website, you can view it on and the about video is on";

// Function to return video url from string

  function extract($html)

  $regex = '/(http:|https:|)\/\/(player.|www.)?(vimeo\.com|youtu(be\.com|\.be|be\.googleapis\.com))\/(video\/|embed\/|channels\/(?:\w+\/)|watch\?v=|v\/)?([A-Za-z0-9._%-]*)(\&\S+)?/';

  preg_match_all($regex, $html, $match);

  $matched = array_unique($match[0]);

  usort($matched, function($a, $b) 


      return strlen($b) - strlen($a);


  return $matched;


// calls function, returns the youtube or vimeo url from a string.

$check_extract = extract($sample_url ); 

// function to find the video provider name.

function videoType($url) {

if (strpos($url, 'youtube') > 0)

  return 'youtube';

else if (strpos($url, 'vimeo') > 0) 

  return 'vimeo';


  return 'unknown';



// calls function, has extracted url as a parameter.

$provider = videoType($check_extract[0]);

// The following regex will extract the video id from above extracted youtube url.


preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/",$check_extract[0], $matches);?>
$id =$matches[1];


else if($provider=="vimeo")


preg_match("/(https?:\/\/)?(www\.)?(player\.)?vimeo\.com\/([a-z]*\/)*([0-9]{6,11})[?]?.*/",$check_extract[0], $output_array);?>

$id =$output_array[5];


// this will get video id of youtube/vimeo.

$video_id = $id;

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download