2014-07-01 41 views
0

使用的preg_replace作秀視頻中,我在YouTube上顯示的視頻,我使用的preg_replace用於替換任何**link**<iframe src="link"></iframe>見代碼檢查元素在Firefox上的HTML代碼是正確的,但它不工作,我不知道爲什麼。我的代碼如下。感謝您的幫助或建議。我怎麼能在YouTube上

$string = "This is video on youtube **http://www.youtube.com/watch?v=wKTBUmrwYDs**"; 
$string = preg_replace("/\*\*([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])\*\*/i",'<iframe width="420" height="315" src="$1" frameborder="0" allowfullscreen></iframe>',$string); 
echo $string; 

而這也行不通。

$string = preg_replace("/\*\*([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])\*\*/i","<object width=\"420\" height=\"315\"><param name=\"movie\" value=\"$1?hl=en_US&amp;version=3&amp;rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"$1?hl=en_US&amp;version=3&amp;rel=0\" type=\"application/x-shockwave-flash\" width=\"420\" height=\"315\" allowscriptaccess=\"always\" allowfullscreen=\"true\"></embed></object>",$string); 

回答

0

這應該工作:

<?php 
$string = "This is video on youtube **http://www.youtube.com/watch?v=wKTBUmrwYDs**"; 

preg_match('#http(s?)://(((www\.)?youtube\.com)|(youtu\.be))/((watch.*(\?|\&)v=([^&]+))|((embed/)?([a-z0-9\-_]+))|)#i' , $string , $matches); 

$youtube_id = array_pop($matches); 

$iframe_tag = '<iframe width="420" height="315" src="'.$youtube_id.'" frameborder="0" allowfullscreen></iframe>';