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 に答える