次のコードを使用して、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);
}