PHP /正则表达式:从YouTube提取视频ID url-嵌入代码
问题描述:
我想从一个YouTube网址输入获取嵌入的代码。我遇到了这个ANSWER,但现在我应用了我的代码的答案,我没有得到任何结果。我的目标是让正则表达式使URL成为:http://www.youtube.com/watch?v=videoid。我将如何获得正则表达式函数来实际处理我的代码?PHP /正则表达式:从YouTube提取视频ID url-嵌入代码
<html>
<form method="post" action="">
URL:
<input type="text" value="<?php $text ?>" name="yurl"> <!--$vari-->
<br>
<!--<br>
Height:
<input type="text" value="<?php $hth ?>" name="yheight"> $hth
<br>-->
<!--<br>
Width:
<input type="text" value="<?php $wdth ?>" name="ywidth"> $wdth
<br>
<br>-->
Autoplay:
<input type="checkbox" value="<?php $auto1; $auto2; ?>" name="yautop"> <!--$auto1 $auto2-->
<br>
<input type="submit" value="Generate Embed Code" name="ysubmit">
<br>
</form>
</html>
PHP
<?php
$vari ='';
if($_POST)
{
function linkifyYouTubeURLs($vari) {
$vari = preg_replace('~
# Match non-linked youtube URL in the wild. (Rev:20111012)
https?:// # Required scheme. Either http or https.
(?:[0-9A-Z-]+\.)? # Optional subdomain.
(?: # Group host alternatives.
youtu\.be/ # Either youtu.be,
| youtube\.com # or youtube.com followed by
\S* # Allow anything up to VIDEO_ID,
[^\w\-\s] # but char before ID is non-ID char.
) # End host alternatives.
([\w\-]{11}) # $1: VIDEO_ID is exactly 11 chars.
(?=[^\w\-]|$) # Assert next char is non-ID or EOS.
(?! # Assert URL is not pre-linked.
[?=&+%\w]* # Allow URL (query) remainder.
(?: # Group pre-linked alternatives.
[\'"][^<>]*> # Either inside a start tag,
| </a> # or inside <a> element text contents.
) # End recognized pre-linked alts.
) # End negative lookahead assertion.
[?=&+%\w-]* # Consume any URL (query) remainder.
~ix',
'<a href="http://www.youtube.com/watch?v=$1">YouTube link: $1</a>',
$vari);
return $vari;
}
$vari = $_POST['yurl'];
$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
$is_auto = 0;
$step1 =explode ('v=', $vari);
$step2 =explode ('&',$step1[1]);
if(isset($_POST['yautop']))
{
if($_POST['yautop'] == 1)
$is_auto = 1;
}
$auto1 = '?autoplay='.$is_auto;
$auto2 = '&autoplay='.$is_auto;
?>
<p>Iframe Result:</p>
<?
//Iframe code with optional autoplay
echo ('<iframe src="http://www.youtube.com/embed/'.$step2[0].'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>
答
也许我失去了一些东西,但岂不是一个更容易使用parse_url
和parse_str
:
$url = 'http://www.youtube.com/watch?v=Imh0vEnOMXU&feature=g-vrec'; // some youtube url
$parsed_url = parse_url($url);
/*
* Do some checks on components if necessary
*/
parse_str($parsed_url['query'], $parsed_query_string);
$v = $parsed_query_string['v'];
当然,如果您输入完整的代码片段youtube提供,您需要一个DOM解析器才能访问该url。
答
你似乎忘了在你的HTML使用 “回声” 来输出变量。
<?php echo $text ?>
和:如果你改变了内联PHP到它的工作
<?php echo $auto1 . $auto2; ?>
这最后一个我改成了两个字符串一起切,他们似乎试图做同样的事情,所以也许你只需要其中的一个,但是如果你想让他们两个都相互印刷,我改变它应该为此工作。
这有帮助吗?
+1不,你没有错过任何东西,我是唯一一个错过了这个重要信息的人。但是如果用户输入这样的链接会发生什么:'http:// www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo'到输入框 – techAddict82 2012-07-11 17:14:22
@charlieCodex我不知道符号,但当$ v变成空白时,您需要一个后退方法。 '$ parsed_url ['fragment']'将包含散列标记之后的所有内容,因此您可以检查它,'/ explode'并获取最后一个数组元素,但是您需要检查其他可能的url是否是有效。 – jeroen 2012-07-11 17:29:20