2011-11-02 22 views
0

我有一個YouTube視頻嵌入代碼,看起來像這樣:如何在PHP中執行此REGEX?

<iframe width="560" height="315" src="http://www.youtube.com/embed/XGdfNb15h9o" frameborder="0" allowfullscreen></iframe> 

我需要添加以下(它擺脫的相關視頻建議):

?rel=0 

的每個網址的結尾在嵌入代碼中。所以在上面的例子中,最終的代碼應該是這樣的:

<iframe width="560" height="315" src="http://www.youtube.com/embed/XGdfNb15h9o?rel=0" frameborder="0" allowfullscreen></iframe> 

的嵌入代碼被存儲在一個名爲$embed_code變量。我如何將嵌入代碼轉換爲適用於它的?rel=0

回答

2

如果一切停留不變,你可以使用str_replace函數代替它會更快。

<?php 
$iframe = '<iframe width="560" height="315" src="http://www.youtube.com/embed/XGdfNb15h9o" frameborder="0" allowfullscreen></iframe>'; 
$pattern = '" frameborder="0"'; 
$replace = '?rel=0" frameborder="0"'; 

$iframe = str_replace($pattern,$replace, $iframe); 

echo $iframe; 

?> 
+0

工程。輸出是板條貓的視頻...大聲笑 – xbonez

1

沒有更多的信息或源代碼,我不認爲你需要正則表達式。爲什麼不這樣做:

$embed_code.= "?rel=0"; 

在輸出HTML之前?

+0

也許iframe的代碼是從別的地方用戶複製粘貼?這個問題是直接的imho – Evert

+0

@Adam Field變量包含整個iframe嵌入代碼,而不僅僅是url。這就是他們存儲在我正在使用的數據庫中的方式。 – TK123

+2

IMO,OP意味着'$ embed_code'從'