Get YouTube video ID from URL w/ PHP(从带有 PHP 的 URL 获取 YouTube 视频 ID)
问题描述
我正在尝试创建一个函数,该函数从 Wordpress 自定义字段(YouTube 视频 URL 的_videourl")调用值,然后使用 PHP 修剪将其缩减为仅 YouTube 视频 ID.我发现了一个 javascript 函数,可以将 URL 缩减为仅 ID,但我不知道如何将其转换为 php(下面的函数):
I'm trying to create a function that calls a value from a Wordpress custom field ("_videourl" for a YouTube video URL) and then uses PHP trim to cut it down to just the YouTube video ID. I found a javascript function that cuts down URLs to just the ID but I have no idea how I'd be able to translate that into php (function below):
function youtubeIDextract(url)
{
var youtube_id;
youtube_id = url.replace(/^[^v]+v.(.{11}).*/,"$1");
return youtube_id;
}
这个 PHP 函数将在循环内使用,所以我认为我将不得不使用变量,但我真的只是一个菜鸟,所以我不知道该怎么做.任何人都可以通过分享他们的编码专业知识来帮助我创建 PHP 函数吗?
This PHP function would be used inside the loop so I think I would have to use variables, but I'm really just a noob so I have no idea what to do. Can anyone help by sharing their coding expertise in helping me create a PHP function?
已解决
经过一些实验,我找到了解决方案.我想返回并发布它,以便其他有需要的人可以从某个地方开始.
After some experimentation, I found a solution. I wanted to return and post it so that others also in need would have somewhere to start from.
function getYoutubeId($ytURL)
{
$urlData = parse_url($ytURL);
//echo '<br>'.$urlData["host"].'<br>';
if($urlData["host"] == 'www.youtube.com') // Check for valid youtube url
{
$ytvIDlen = 11; // This is the length of YouTube's video IDs
// The ID string starts after "v=", which is usually right after
// "youtube.com/watch?" in the URL
$idStarts = strpos($ytURL, "?v=");
// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
// bases covered), it will be after an "&":
if($idStarts === FALSE)
$idStarts = strpos($ytURL, "&v=");
// If still FALSE, URL doesn't have a vid ID
if($idStarts === FALSE)
die("YouTube video ID not found. Please double-check your URL.");
// Offset the start location to match the beginning of the ID string
$idStarts +=3;
// Get the ID string and return it
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
}
else
{
//echo 'This is not a valid youtube video url. Please, give a valid url...';
return 0;
}
}
推荐答案
我不得不在几周前写的一个 PHP 类中处理这个问题,最终得到一个匹配任何类型字符串的正则表达式:有或没有 URL方案,带或不带子域、youtube.com URL 字符串、youtu.be URL 字符串以及处理各种参数排序.您可以在 在 GitHub 上查看或者简单地复制并粘贴下面的代码块:
I had to deal with this for a PHP class i wrote a few weeks ago and ended up with a regex that matches any kind of strings: With or without URL scheme, with or without subdomain, youtube.com URL strings, youtu.be URL strings and dealing with all kind of parameter sorting. You can check it out at GitHub or simply copy and paste the code block below:
/**
* Check if input string is a valid YouTube URL
* and try to extract the YouTube Video ID from it.
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @param $url string The string that shall be checked.
* @return mixed Returns YouTube Video ID, or (boolean) false.
*/
function parse_yturl($url)
{
$pattern = '#^(?:https?://)?(?:www.)?(?:youtu.be/|youtube.com(?:/embed/|/v/|/watch?v=|/watch?.+&v=))([w-]{11})(?:.+)?$#x';
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}
为了解释正则表达式,这里有一个溢出的版本:
To explain the regex, here's a spilt up version:
/**
* Check if input string is a valid YouTube URL
* and try to extract the YouTube Video ID from it.
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @param $url string The string that shall be checked.
* @return mixed Returns YouTube Video ID, or (boolean) false.
*/
function parse_yturl($url)
{
$pattern = '#^(?:https?://)?'; # Optional URL scheme. Either http or https.
$pattern .= '(?:www.)?'; # Optional www subdomain.
$pattern .= '(?:'; # Group host alternatives:
$pattern .= 'youtu.be/'; # Either youtu.be,
$pattern .= '|youtube.com'; # or youtube.com
$pattern .= '(?:'; # Group path alternatives:
$pattern .= '/embed/'; # Either /embed/,
$pattern .= '|/v/'; # or /v/,
$pattern .= '|/watch?v='; # or /watch?v=,
$pattern .= '|/watch?.+&v='; # or /watch?other_param&v=
$pattern .= ')'; # End path alternatives.
$pattern .= ')'; # End host alternatives.
$pattern .= '([w-]{11})'; # 11 characters (Length of Youtube video ids).
$pattern .= '(?:.+)?$#x'; # Optional other ending URL parameters.
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}
这篇关于从带有 PHP 的 URL 获取 YouTube 视频 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从带有 PHP 的 URL 获取 YouTube 视频 ID
基础教程推荐
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- HTTP 与 FTP 上传 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01