2012-04-27 48 views
0

我使用以下功能來顯示youtube鏈接作爲視頻,但問題是,我不知道替換多個視頻,所以我怎麼能限制它只有1?嵌入第一個唯一的YouTube視頻

PHP代碼:

function embedYoutube($text) 
{ 

    $pattern = '/[\\?\\&]v=([^\\?\\&]+)/'; 
    $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
return preg_replace($pattern, $replace, $text, 1); 

preg_replace第四個參數是替換的極限。

+0

這很容易,謝謝。 – fxuser 2012-04-27 15:56:38