PHP / Regex:从youtube url嵌入代码中提取视频ID
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP / Regex:从youtube url嵌入代码中提取视频ID,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2760字,纯文字阅读大概需要4分钟。
内容图文
![PHP / Regex:从youtube url嵌入代码中提取视频ID](/upload/InfoBanner/zyjiaocheng/777/4a0732fc59374d158ebb4e3505139bb3.jpg)
我试图从youtube网址输入中获取嵌入代码.我遇到了这个ANSWER,但现在我将这个答案应用到我的代码中我得不到任何结果.我的目标是让正则表达式将URL设置为:http://www.youtube.com/watch?v=videoid.我如何能够使用正则表达式函数实际使用我的代码?
<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.
内容总结
以上是互联网集市为您收集整理的PHP / Regex:从youtube url嵌入代码中提取视频ID全部内容,希望文章能够帮你解决PHP / Regex:从youtube url嵌入代码中提取视频ID所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。