我目前正在使用我的函数getYoutubeId提取youtube视频I。该函数通常会解析链接以找到其各自的ID。但我在为输入的文本框分配一个php变量时遇到了问题,该变量会将url传递给该函数,并最终提取ID。当将url粘贴到文本框中时,视频ID或根本没有回显。下面是一个示例:Mockup SITE
<input type="text" name="youtube" value="<? $sYoutubeUrl ?>">
<input type="submit" value="Parsen">
<?
function getYoutubeId($sYoutubeUrl) {
# set to zero
$youtube_id = "";
$sYoutubeUrl = trim($sYoutubeUrl);
# the User entered only the eleven chars long id, Case 1
if(strlen($sYoutubeUrl) === 11) {
$youtube_id = $sYoutubeUrl;
return $sYoutubeUrl;
}
# the User entered a Url
else {
# try to get all Cases
if (preg_match('~(?:youtube\.com/(?:user/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})~i', $sYoutubeUrl, $match)) {
$youtube_id = $match[1];
return $youtube_id;
}
# try to get some other channel codes, and fallback extractor
elseif(preg_match('~http://www.youtube.com/v/([A-Za-z0-9\-_]+).+?|embed\/([0-9A-Za-z-_]{11})|watch\?v\=([0-9A-Za-z-_]{11})|#.*/([0-9A-Za-z-_]{11})~si', $sYoutubeUrl, $match)) {
for ($i=1; $i<=4; $i++) {
if (strlen($match[$i])==11) {
$youtube_id = $match[$i];
break;
}
}
return $youtube_id;
}
else {
$youtube_id = "No valid YoutubeId extracted";
return $youtube_id;
}
}
}
echo (getYoutubeId($sYoutubeUrl));
?>发布于 2013-02-10 04:23:46
您需要将输入标记放在表单中,如下所示:
<form action="" method="POST">
<input type="text" name="youtube" value="" />
<input type="submit" value="Parsen" />
</form>然后检查表单是否已提交,并显示您的信息:
?>
if(isset($_POST['youtube'])){
// include that function here
print 'Result: ' . getYoutubeId($_POST['youtube']);
}考虑使用parse_url()从URL中获取视频ID,而不是正则表达式。
https://stackoverflow.com/questions/14791137
复制相似问题