Note to self: Youtube ID from regex PHP snippet

$subject[]="https://www.youtube.com/watch?v=61QF9CgzRQI&feature=youtu.be";
$subject[]="http://youtu.be/61QF9CgzRQI";
$subject[]="http://www.youtube.com/embed/61QF9CgzRQI?rel=0";

foreach($subject as $sub){
	$preg_test = preg_match("/.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/", $sub,$matches);
	?>
	<pre><?php print_r($matches)?></pre>
	<?php
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *