我目前正试图通过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