0

我使用以下代码替换 youtube url 以嵌入链接,以便将它们显示为视频。但是我遇到了一个问题,我无法从具有除 ?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);

}
4

1 回答 1

0

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

|http://www\.youtube\.com/watch\?.*?\bv=([^ ]+)\&?|
于 2012-04-27T16:30:51.003 回答