【发布时间】:2012-07-11 10:03:02
【问题描述】:
可能重复:
php regex - find all youtube video ids in string
Get YouTube video ID from URL w/ PHP
我发现 youtube 可能有几种显示 URL 的格式。我创建了一些 php 代码,仅当链接仅采用此 url 格式时才提取视频 ID:http://www.youtube.com/watch?v=videoid。
我发现这个 REGEX 用于提取 youtube 视频 ID,但我不确定如何将它应用到我的代码中。我已将它放在我的代码中,但没有任何反应。如何将它应用到我的代码或整体上的不同方法?
HERE 是我的网站。
但它无法从该 url 格式中获取视频 ID:
latest short format: http://youtu.be/NLqAF9hrVbY
iframe: http://www.youtube.com/embed/NLqAF9hrVbY
iframe (secure): https://www.youtube.com/embed/NLqAF9hrVbY
object param: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
object embed: http://www.youtube.com/v/NLqAF9hrVbY?fs=1&hl=en_US
watch: http://www.youtube.com/watch?v=NLqAF9hrVbY
users: http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo
ytscreeningroom: http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I
any/thing/goes!: http://www.youtube.com/sandalsResorts#p/c/54B8C800269D7C1B/2/PPS-8DMrAn4
any/subdomain/too: http://gdata.youtube.com/feeds/api/videos/NLqAF9hrVbY
more params: http://www.youtube.com/watch?v=spDj54kf-vY&feature=g-vrec
PHP
<?php
$vari ='';
if($_POST)
{
$url = $_POST['yurl'];
$parsed_url = parse_url($url);
parse_str($parsed_url['query'], $parsed_query_string);
$v = $parsed_query_string['v'];
$hth = 300; //$_POST['yheight'];
$wdth = 500; //$_POST['ywidth'];
$is_auto = 0;
?>
<p>Iframe Result:</p>
<?
//Iframe code with optional autoplay
echo ('<iframe src="http://www.youtube.com/embed/'.$v.'" frameborder="0" width="'.$wdth.'" height="'.$hth.'"></iframe>');
?>
<p>Old way to embedd result: </p>
<?
//Old way to embed code- optional autoplay
echo ('<embed width="'.$wdth.'" height="'.$hth.'" type="application/x-shockwave-flash" src="http://www.youtube.com/v/'.$v.'" wmode="transparent" embed="" /></embed>');
}
?>
HTML
<form method="post" action="">
URL:
<input type="text" value="<?php $url ?>" name="yurl"> <!--$vari-->
<br>
<input type="submit" value="Generate Embed Code" name="ysubmit">
<br>
</form>
【问题讨论】:
-
你做了什么调试?你能显示minimal test-case吗?
-
你需要输出url:
<?php $url ?>应该是<?php echo $url; ?>