youtube link regex

雨燕双飞 提交于 2020-01-02 13:28:47

问题


im trying to make this work for all youtube links, which is working, except for youtube.com/v/videoid

only watch?v= is working with this regex, any assistance is greatful.

/http:\/\/(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#\!)v=)([\w-]{11}).*/i

回答1:


/((http:\/\/)?(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/v\/)([\w-]{11}).*|http:\/\/(?:youtu\.be\/|(?:[a-z]{2,3}\.)?youtube\.com\/watch(?:\?|#\!)v=)([\w-]{11}).*)/i



回答2:


The website Regular Expression Library is a good source for that. Try the following link: http://www.regexlib.com/Search.aspx?k=youtube




回答3:


this is my first contribution, hope it helps.

$url = "https://www.youtube-nocookie.com/embed/lCpaCAfhCH4";

if (preg_match("/^((https?:\/\/)?(w{0,3}\.)?youtu(\.be|(be|be-nocookie)\.\w{2,3}\/))((watch\?v=|v|embed)?[\/]?(?P<video>[a-zA-Z0-9-_]{11}))/si", $url, $matches)) {

echo $matches['video'];

}

This regex works with any youtube url format (long, short, embed, secure, etc)

Thanks!




回答4:


done use regular expression. This is better way to grab the video ID

<?php
$url = "http://www.youtube.com/watch?v=KkMDCCdjyW8&feature=relate";
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_vars );
echo $my_array_of_vars['v'];    
  // Output: KkMDCCdjyW8
?>

working here



来源:https://stackoverflow.com/questions/4545427/youtube-link-regex

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