Regex: Removing Youtube Video ID from URL

I am currently using regex code to separate YT video identifiers. The reason I use regex is because yt urls are different in many formats. I created a regular expression that pretty much detects the identifier of almost all ur YT formats except the one below. I tried modifying it, but no luck. Is there a way for Regex to split the id from the URL below?

http://www.youtube.com/watch?feature=v-feature&v=317a815FLWQ

Regex:

('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/| youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>| </a>))[?=&+%\w]*~ix','http://www.youtube.com/watch?v=$1',$url);
+5
source share
4 answers

? "v =" "&".? , URL-

+3

:

(&|\?)v=(\w*)(&|$)

, & ?, , & .

PHP, , . . , , , URL.

preg_match('(&|\?)v=(\w*)(&|$)', $url, $matches);
$res = 'http://www.youtube.com/watch?v=' + $matches[1]

$res URL, .

0

FYI , , :

    function getYouTubeId($url)
    {
        $pattern = '#^(?:https?://|//)?(?:www\.|m\.)?(?:youtu\.be/|youtube\.com/(?:embed/|v/|watch\?v=|watch\?.+&v=))([\w-]{11})(?![\w-])#';
        preg_match($pattern, $url, $matches);
        return (isset($matches[1])) ? $matches[1] : false;
    }

:

http://www.youtube.com/watch?v=-wtIMTCHWuI
http://www.youtube.com/v/-wtIMTCHWuI?version=3&autohide=1
http://youtu.be/-wtIMTCHWuI
https://www.youtube.com/embed/-wtIMTCHWuI

The function originally found from this post some time ago.

0
source

Why would you just

$url = "http://www.youtube.com/watch?feature=v-feature&v=317a815FLWQ&hello=ok";
$stop = strlen($url);
$pos = strpos($url,'v=')+2;
$x = strpos($url,'&',$pos);
if($x)
    {
    $x = $x - $pos;
    $stop = $x;
    }
$str = substr($url,$pos,$stop);
echo $str;

It basically always starts with v = ....

-1
source

All Articles