【问题标题】:embed youtube video parameters ignored嵌入 youtube 视频参数被忽略
【发布时间】:2012-05-08 10:07:19
【问题描述】:

我使用以下代码替换 youtube 网址以嵌入链接,以便将它们显示为视频。但是我遇到了一个问题,我无法从具有更多参数的链接查看视频,而不是 ?v=video_code

php代码:

function embedYoutube($text)
{

    $pattern = '|http://www\.youtube\.com/watch\?.*?\bv=([^ ]+)|';
    $replacement = '<div style="width:100%;float:left;margin-top:15px;margin-bottom:15px;"><iframe width="570" height="315" src=http://www.youtube.com/embed/$1 frameborder="0" allowfullscreen></iframe></div>';
    return preg_replace($pattern, $replacement, $text, 1);

}

【问题讨论】:

    标签: php regex preg-replace pattern-matching embed


    【解决方案1】:

    更新您的正则表达式以仅匹配 v 参数,并且在第一个 &amp; 之前:

    |http://www\.youtube\.com/watch\?.*?\bv=([^ ]+)\&?|
    

    【讨论】:

    猜你喜欢
    • 2012-02-22
    • 1970-01-01
    • 2021-12-04
    • 2012-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多