我目前正試圖通過url檢索YouTube視頻ID。我創建了一個函數getYoutubeVideoID
,它剝離了$url
並找到了$sVideoID
。問題是,當我回顯變量時,我得到一個空值$ sVideoID。如果我將視頻ID分配給$sVideoID
,我如何得到沒有價值的結果?PHP顯示來自YouTube的視頻ID
<?php
if($_POST)
{
$url = $_POST['yurl'];
function getYoutubeVideoID($url) {
$sVideoID = preg_replace('~https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/| youtube\.com\S*[^\w\-\s])([\w\-]{11})
(?=[^\w\-]|$)(?![?=&+%\w]*(?:[\'"][^<>]*>| </a>))[?=&+%\w-]*~ix','<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',$url);
return $sVideoID;
}
$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
?>
<?
//Iframe code
echo htmlentities ('<iframe src="http://www.youtube.com/embed/'.$sVideoID.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>
<?
//Old way to embed code
echo htmlentities ('<embed src="http://www.youtube.com/v/'.$sVideoID.'" width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash" wmode="transparent" embed="" /></embed>');
}
?>
你可能在URL其他參數和順序不確定性。例如:http://www.youtube.com/watch?feature=g-all-esi&v=6mgFdn4lfrE – alfasin 2012-07-11 21:20:44
你在哪裏設置'$ sVideoID'? – 2012-07-11 21:31:36
可能重複的[PHP的正則表達式的YouTube視頻ID](http://stackoverflow.com/questions/11438544/php-regex-for-youtube-video-id) – mario 2012-07-11 22:45:17