从动态生成的youtu.be链接中获取youtube视频ID(Get youtube video ID from a dynamically generated youtu.be link)

编程入门 行业动态 更新时间:2024-10-17 05:28:46
从动态生成的youtu.be链接中获取youtube视频ID(Get youtube video ID from a dynamically generated youtu.be link)

我正在使用WordPress插件输出文本链接,但用户输入来自第三方网站的链接。 链接是youtu.be格式,我需要视频的ID。 我尝试过这种方法,但到目前为止我还没有尝试过

原始代码:

if($video_link != "") { echo '<p>'; echo 'Please visit : <a href="'. $video_link .'"> Multimedia link </a> for more photos and information' ; echo '</p>'; }

输出链接:

http://youtu.be/abcdefghijkl

试过这个,不能让它工作:

if($video_link != "") { $url = $_GET['url']; $video_id = substr( parse_url($url, PHP_URL_PATH), 1 ); echo '<iframe width="560" height="315" src="https://www.youtube.com/embed'.$video_id.'" frameborder="0" allowfullscreen></iframe>' ; }

输出iframe不起作用:

I'm working with a WordPress plugin that outputs a text link, but the user inputs the link from an 3rd party site. the links are youtu.be format, I need the video's ID. I have tried this method but I'm not getting it so far

original code:

if($video_link != "") { echo '<p>'; echo 'Please visit : <a href="'. $video_link .'"> Multimedia link </a> for more photos and information' ; echo '</p>'; }

Output link :

http://youtu.be/abcdefghijkl

tried this, can't get it to work:

if($video_link != "") { $url = $_GET['url']; $video_id = substr( parse_url($url, PHP_URL_PATH), 1 ); echo '<iframe width="560" height="315" src="https://www.youtube.com/embed'.$video_id.'" frameborder="0" allowfullscreen></iframe>' ; }

Output Iframe does not work :

最满意答案

编辑:忽略我以前的答案,你的解析工作正常。 你忘记了iframe src属性中的'embed'之后的/ 。

或者如果你愿意,你可以删除你的解析中的substr部分(这是剥离斜线的部分)

$video_id = parse_url($url, PHP_URL_PATH);

EDIT: Ignore my previous answer, your parse works fine. You just forgot the / after 'embed' in your iframe src attribute.

Or if you want, you can just remove the substr part in your parse (which is what strips the slash)

$video_id = parse_url($url, PHP_URL_PATH);

更多推荐

本文发布于:2023-04-28 03:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329734.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:链接   动态   视频   generated   youtu

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!