parse youtube video id using preg_match

Posted by Webbo on Stack Overflow See other posts from Stack Overflow or by Webbo
Published on 2010-05-29T20:19:42Z Indexed on 2010/05/29 20:22 UTC
Read the original article Hit count: 262

Filed under:
|
|
|
|

Hi,

I am attempting to parse the video ID of a youtube URL using preg_match. I found a regular expression on this site that appears to work;

(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+

As shown in this pic; http://i.imgur.com/SQJW2.jpg

My PHP is as follows, but it doesn't work (gives Unknown modifier '[' error)...

<?
 $subject = "http://www.youtube.com/watch?v=z_AbfPXTKms&NR=1";

 preg_match("(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=[0-9]/)[^&\n]+|(?<=v=)[^&\n]+", $subject, $matches);

 print "<pre>";
 print_r($matches);
 print "</pre>";

?>

Cheers

© Stack Overflow or respective owner

Related posts about php

Related posts about regex