如果在字符串中找到了此函數,該函數會嵌入youtube視頻。用HTML嵌入代碼替換文本中的YouTube網址
我的問題是什麼是最簡單的方法來捕獲嵌入式視頻(iframe和只有第一個,如果有更多的),並忽略其餘的字符串。
function youtube($string,$autoplay=0,$width=480,$height=390)
{
preg_match('#(v\/|watch\?v=)([\w\-]+)#', $string, $match);
return preg_replace(
'#((http://)?(www.)?youtube\.com/watch\?[=a-z0-9&_;-]+)#i',
"<div align=\"center\"><iframe title=\"YouTube video player\" width=\"$width\" height=\"$height\" src=\"http://www.youtube.com/embed/$match[2]?autoplay=$autoplay\" frameborder=\"0\" allowfullscreen></iframe></div>",
$string);
}
最簡單,最穩健的方法是不使用正則表達式。 – FailedDev
@FailedDev小心告訴我如何(不一定是相同的功能)? – domino
您正在將$ string的部分傳遞給$ string嗎?你怎麼得到這個字符串? – FailedDev