【问题标题】:How to make direct youtube video url work not embeded?如何使直接 youtube 视频 url 工作不嵌入?
【发布时间】:2018-01-09 10:00:43
【问题描述】:

如何在没有嵌入代码的情况下使 youtube 视频在现场工作?我的意思是直接代码为:https://www.youtube.com/watch?v=ouDmKW1FGjo。我希望用户将直接视频网址粘贴到字段 VIDEO_LINK 而不是嵌入代码中。我怎样才能做到这一点?只有嵌入链接适用于下面的代码而不是直接链接。

<iframe width="100%" height="550" src="<?php the_field('video_link') ?>" frameborder="0" allowfullscreen></iframe>

【问题讨论】:

标签: php wordpress url video youtube


【解决方案1】:

使用以下代码获取视频密钥(假设网址为https://www.youtube.com/watch?v=ouDmKW1FGjo):

<?php
    $video_url=get_field('video_link');

    $url = urldecode(rawurldecode($video_url));
    preg_match("/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:m\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/", $url, $matches);

      // Get key of youtube by preg_match and put it in iframe 

      $videoKey= $matches[1];
 ?>
<iframe width="100%" height="550" src="https://www.youtube.com/embed/<?php echo $videoKey ;?>" frameborder="0" allowfullscreen></iframe>

【讨论】:

  • 好。如果可以,请点击绿色对勾上方的向上箭头。
【解决方案2】:

您可以使用此功能将 Youtube URL 转换为嵌入代码:

function get_youtube_embed($youtube_url, $width=560, $height=315)
{
    $height = (int)$height;
    $width = (int)$width;
    $embed_html = '';
            $parts = parse_url($youtube_url);
            if(isset($parts['query'])) {
                    parse_str($parts['query'], $query);
                    if(isset($query['v'])) {
                            $embed_html = '<iframe width="'.$width.'" height="'.$height.'" src="https://www.youtube.com/embed/'.$query['v'].'" frameborder="0" allowfullscreen></iframe>';
                    }
            }
            return $embed_html;
}

【讨论】:

    猜你喜欢
    • 2017-01-09
    • 2012-04-07
    • 2016-01-19
    • 2022-06-30
    • 2011-09-10
    • 2017-09-14
    • 2021-04-14
    • 2012-10-12
    • 1970-01-01
    相关资源
    最近更新 更多