Get youtube id for all url types

别来无恙 提交于 2019-12-04 17:49:26

Your regex appears to solve the problem as it stands now? I didn't try it in php, but it appears to work fine in my editor.

The first part of the regex http://(?:www\.)?youtu\.?be(?:\.com)?/matches http://youtu.be/ and the second part (embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=|) ends with |) which means it matches nothing (making it optional). In other words it would trim away http://youtu.be/ leaving only the id.

A more intuitive way of writing it would be to make the whole if grouping optional I suppose, but as far as I can tell your regex is already solving your problem:

 #http://(?:www\.)?youtu\.?be(?:\.com)?/(embed/|watch\?v=|\?v=|v/|e/|.+/|watch.*v=)?#i

Note: Your regex would work with the www.youtu.be.com domain as well. It would be stripped away, but something to watch out for if you use this for validating input.

Update:

If you want to only match urls inside [youtube][/youtube] tags you could use look arounds. Something along the lines of:

(?<=\[youtube\])(?:http://(?:www\.)?youtu\.?be(?:\.com)?/(?:embed/|watch\?v=|\?v=|v/|e/|[^\[]+/|watch.*v=)?)(?=.+\[/youtube\])

You could further refine it by making the .+ in the look ahead only match valid URL characters etc.

//$url = 'http://www.youtube.com/watch?v=ZedLgAF9aEg';
$url = 'http://youtu.be/ZedLgAF9aEg';

if (FALSE === strpos($url, 'youtu.be/')) {
    parse_str(parse_url($url, PHP_URL_QUERY), $id);
    $id = $id['v'];
} else {
    $id = basename($url);
}

echo $id; // ZedLgAF9aEg

Will work for both versions of URLs. Do not use regex for this as PHP has built in functions for parsing URLs as I have demonstrated which are faster and more robust against breaking.

Try this, hope it'll help you

function YouTubeUrl($url)
{
if($url!='')
{
    $newUrl='';
    $videoLink1=$url;
    $findKeyWord='youtu.be';
    $toBeReplaced='www.youtube.com';
    if(IsContain('watch?v=',$videoLink1))
    {
        $newUrl=tMakeUrl($videoLink1);
    }
    else if(IsContain($videoLink1, $findKeyWord))
    {
        $videoLinkArray=explode('/',$videoLink1);
        $Protocol='';
        if(IsContain('://',$videoLink1))
        {
            $protocolArray=explode('://',$videoLink1);
            $Protocol=$protocolArray[0];
        }

        $file=$videoLinkArray[count($videoLinkArray)-1];
        $newUrl='www.youtube.com/watch?v='.$file;
        if($Protocol!='')
            $newUrl.=$Protocol.$newUrl;
        else
            $newUrl=tMakeUrl($newUrl);
    }
    else
        $newUrl=tMakeUrl($videoLink1);
    return $newUrl;
}
return '';
}

function IsContain($string,$findKeyWord)
{
     if(strpos($string,$findKeyWord)!==false)
    return true;
     else
    return false;
}

function tMakeUrl($url)
{
$tSeven=substr($url,0,7);
$tEight=substr($url,0,8);
if($tSeven!="http://" && $tEight!="https://")
{
    $url="http://".$url;
}
return $url;
}

You can use bellow function for any of youtube URL

I hope this will help you

function checkYoutubeId($id)

{

$youtube = "http://www.youtube.com/oembed?url=". $id ."&format=json";

$curl = curl_init($youtube);

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$return = curl_exec($curl);

curl_close($curl);

return json_decode($return, true);

}

This function return Youtube video detail if Id match to youtube video ID

A little improvement to @rvalvik answer would be to include the case of the mobile links (I've noticed it while working with a customer who used an iPad to navigate, copy and paste links). In this case, we have a m (mobile) letter instead of www. Regex then becomes:

#(https?://)?(?:www\.)?(?:m\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch?.*?v=))([\w\-]{10,12}).*#x

Hope it helps.

NaughtySquid

A slight improvement of another answer:

if (strpos($url, 'feature=youtu.be') === TRUE || strpos($url, 'youtu.be') === FALSE ) 
{
    parse_str(parse_url($url, PHP_URL_QUERY), $id);
    $id = $id['v'];
} 
else 
{
    $id = basename($url);
}

This takes into account youtu.be still being in the URL, but not the URL itself (it does happen!) as it could be the referring feature link.

try this :

$string = explode("=","http://www.youtube.com/watch?v=ZedLgAF9aEg");
echo $string[1];

would turn into: ZedLgAF9aEg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!