Php 如何通过url识别youtube或vimeo站点?

Php 如何通过url识别youtube或vimeo站点?,php,drupal-6,Php,Drupal 6,我在drupal中创建了一个表单,其中有一个名为“URL”的字段。现在,该站点的用户将在此处输入任意url。现在,通过用户输入的url,我只想验证它是否是正确的youtube或vimeo url。例如: 网址:www.youtube.com/video\u Id 网址:www.vimeo.com 我可以通过使用诸如strstr或preg_match之类的php函数轻松识别它。但是,假设有人输入他的博客的URL,其中讲述了youtube,可能如下所示- www.learnyoutube.com或w

我在drupal中创建了一个表单,其中有一个名为“URL”的字段。现在,该站点的用户将在此处输入任意url。现在,通过用户输入的url,我只想验证它是否是正确的youtube或vimeo url。例如:

网址:www.youtube.com/video\u Id 网址:www.vimeo.com

我可以通过使用诸如strstr或preg_match之类的php函数轻松识别它。但是,假设有人输入他的博客的URL,其中讲述了youtube,可能如下所示- www.learnyoutube.com或www.mysite/about_youtube.com等

在上述情况下,我的验证将失败,因为url字符串包含youtube

最后,我只想知道“youtube”和“vimeo”视频的有效格式是什么,这样我就可以相应地应用逻辑了


如果有人还有其他想法,请帮助。

您可以在url开头匹配域部分

preg_match('#^((http://)|(https://)){0,1}(www\.){0,1}youtube\.com#', $url)
这就是preg的工作原理:

^ - everything after this should be at the very beginning
( - start sub pattern which can be "http://" or "https://"
   (http://)
   | - OR       
   (https://)
) - end subpattern 
{0,1} - look for 0 to 1 occurrences of the previous subpattern
(www\.){0,1} - look for 0 to 1 occurrences of "www."
youtube\.com - look for "youtube.com"
它将匹配任何以以下内容开头的内容:

http(s)://www.youtube.com
http(s)://youtube.com
www.youtube.com
youtube.com
编辑:JavaScript版本:

var site= 'http://www.youtube.com';
var match = site.match(/^((http:\/\/)|(https:\/\/)){0,1}(www\.){0,1}youtube\.com/);

如果字符串匹配,match将有一个匹配值数组,如果不匹配,则为null。

您可以只匹配url开头的域部分

preg_match('#^((http://)|(https://)){0,1}(www\.){0,1}youtube\.com#', $url)
这就是preg的工作原理:

^ - everything after this should be at the very beginning
( - start sub pattern which can be "http://" or "https://"
   (http://)
   | - OR       
   (https://)
) - end subpattern 
{0,1} - look for 0 to 1 occurrences of the previous subpattern
(www\.){0,1} - look for 0 to 1 occurrences of "www."
youtube\.com - look for "youtube.com"
它将匹配任何以以下内容开头的内容:

http(s)://www.youtube.com
http(s)://youtube.com
www.youtube.com
youtube.com
编辑:JavaScript版本:

var site= 'http://www.youtube.com';
var match = site.match(/^((http:\/\/)|(https:\/\/)){0,1}(www\.){0,1}youtube\.com/);

如果字符串匹配,match将有一个匹配值数组,如果不匹配,则为null。

我认为您使用的是嵌入字段。如果是,则embed将自动验证正确的youtube或Video。您必须在配置页面url上检查站点。如果没有,那么Pinetree已经正确地解释了它,所以使用它。

我认为您使用的是嵌入字段。如果是,则embed将自动验证正确的youtube或Video。您必须在配置页面url上检查站点。如果没有,那么Pinetree已经正确地解释了它,所以请使用它。

我已经在多个项目中调整了这些正则表达式,以匹配Youtube和Vimeo URL。它们都非常适合匹配,您可以使用PHP的preg_match获取整个URL或视频ID。我在项目中使用它从视频URL获取ID,并格式化一个比默认嵌入提供的更好的Youtube播放器

/**
 * Fetches the ID of a Vimeo video given a URL
 *
 * @param string $source  Vimeo URL
 *
 * @return mixed          A video ID if matched, otherwise false
 */
function video_id_from_vimeo_url($source)
{
    $pattern = "/^(?:(?:https?:\/\/)?(?:www\.)?vimeo\.com.*\/([\w\-]+))/is";
    $matches = array();
    preg_match($pattern, $source, $matches);
    if (isset($matches[1])) return $matches[1];
    return false;
}

/**
 * Fetches the ID of a YouTube video given a URL
 *
 * @param string $source  YouTube URL
 *
 * @return mixed          A video ID if matched, otherwise false
 */
function video_id_from_youtube_url($source)
{
    $pattern = '/^(?:(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtu(?:be\.com|\.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/is';
    $matches = array();
    preg_match($pattern, $source, $matches);
    if (isset($matches[1])) return $matches[1];
    return false;
}

/**
 * Generate a URL to a YouTube video with a YouTube video ID
 *
 * @param string $id     YouTube video ID
 * @param array $params  Query params to send to video
 *
 * @return string        A YouTube URL
 */
function youtube_video_url_with_id($id, array $params = array())
{
    $query = empty($params) ? '' : '?' . http_build_query($params);
    return esc_url("//youtube.com/embed/{$id}{$query}");
}

/**
 * Generate a URL to a Vimeo video with a Vimeo video ID
 *
 * @param string $id     Vimeo video ID
 * @param array $params  Query params to send to video
 *
 * @return string        A Vimeo URL
 */
function vimeo_video_url_with_id($id, array $params = array())
{
    $defaults = array(
        'color'    => 'ffffff',
        'title'    => 0,
        'byline'   => 0,
        'portrait' => 0,
    );
    $params = array_merge($defaults, $params);

    $query = empty($params) ? '' : '?' . http_build_query($params);
    return "//player.vimeo.com/video/{$id}{$query}";
}
应该匹配所有形式的Youtube URL。示例用法:

$url = "https://www.youtube.com/watch?v=buNGJm_s0XE";
$id = video_id_from_youtube_url($url); // buNGJm_s0XE

$url = "http://youtu.be/buNGJm_s0XE";
$id = video_id_from_youtube_url($url); // buNGJm_s0XE

$fmt_url = youtube_video_url_with_id($id);
或Vimeo URL:

$id = video_id_from_vimeo_url('https://vimeo.com/ondemand/afilmaboutcoffee/112360104'); // 112360104
$id = video_id_from_vimeo_url('https://vimeo.com/112360104'); // 112360104
我使用这些函数和正则表达式的组合来匹配正确的格式化程序,然后输出一个漂亮的视频。在我最近的Ember应用程序中,我有一个Javascript组件,它观察URL的变化,然后更新HTML输出以显示视频预览。使用与PHP版本相同的正则表达式。像这样的咖啡脚本:

  YOUTUBE_EXPR = /^(?:(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtu(?:be\.com|\.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/i
  VIMEO_EXPR   = /^(?:(?:https?:\/\/)?(?:www\.)?vimeo\.com.*\/([\w\-]+))/i

我一直在多个项目中调整这些正则表达式,以匹配Youtube和Vimeo URL。它们都非常适合匹配,您可以使用PHP的preg_match获取整个URL或视频ID。我在项目中使用它从视频URL获取ID,并格式化一个比默认嵌入提供的更好的Youtube播放器

/**
 * Fetches the ID of a Vimeo video given a URL
 *
 * @param string $source  Vimeo URL
 *
 * @return mixed          A video ID if matched, otherwise false
 */
function video_id_from_vimeo_url($source)
{
    $pattern = "/^(?:(?:https?:\/\/)?(?:www\.)?vimeo\.com.*\/([\w\-]+))/is";
    $matches = array();
    preg_match($pattern, $source, $matches);
    if (isset($matches[1])) return $matches[1];
    return false;
}

/**
 * Fetches the ID of a YouTube video given a URL
 *
 * @param string $source  YouTube URL
 *
 * @return mixed          A video ID if matched, otherwise false
 */
function video_id_from_youtube_url($source)
{
    $pattern = '/^(?:(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtu(?:be\.com|\.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/is';
    $matches = array();
    preg_match($pattern, $source, $matches);
    if (isset($matches[1])) return $matches[1];
    return false;
}

/**
 * Generate a URL to a YouTube video with a YouTube video ID
 *
 * @param string $id     YouTube video ID
 * @param array $params  Query params to send to video
 *
 * @return string        A YouTube URL
 */
function youtube_video_url_with_id($id, array $params = array())
{
    $query = empty($params) ? '' : '?' . http_build_query($params);
    return esc_url("//youtube.com/embed/{$id}{$query}");
}

/**
 * Generate a URL to a Vimeo video with a Vimeo video ID
 *
 * @param string $id     Vimeo video ID
 * @param array $params  Query params to send to video
 *
 * @return string        A Vimeo URL
 */
function vimeo_video_url_with_id($id, array $params = array())
{
    $defaults = array(
        'color'    => 'ffffff',
        'title'    => 0,
        'byline'   => 0,
        'portrait' => 0,
    );
    $params = array_merge($defaults, $params);

    $query = empty($params) ? '' : '?' . http_build_query($params);
    return "//player.vimeo.com/video/{$id}{$query}";
}
应该匹配所有形式的Youtube URL。示例用法:

$url = "https://www.youtube.com/watch?v=buNGJm_s0XE";
$id = video_id_from_youtube_url($url); // buNGJm_s0XE

$url = "http://youtu.be/buNGJm_s0XE";
$id = video_id_from_youtube_url($url); // buNGJm_s0XE

$fmt_url = youtube_video_url_with_id($id);
或Vimeo URL:

$id = video_id_from_vimeo_url('https://vimeo.com/ondemand/afilmaboutcoffee/112360104'); // 112360104
$id = video_id_from_vimeo_url('https://vimeo.com/112360104'); // 112360104
我使用这些函数和正则表达式的组合来匹配正确的格式化程序,然后输出一个漂亮的视频。在我最近的Ember应用程序中,我有一个Javascript组件,它观察URL的变化,然后更新HTML输出以显示视频预览。使用与PHP版本相同的正则表达式。像这样的咖啡脚本:

  YOUTUBE_EXPR = /^(?:(?:(?:https?:)?\/\/)?(?:www\.)?(?:youtu(?:be\.com|\.be))\/(?:watch\?v\=|v\/|embed\/)?([\w\-]+))/i
  VIMEO_EXPR   = /^(?:(?:https?:\/\/)?(?:www\.)?vimeo\.com.*\/([\w\-]+))/i

谢谢你的回答,但是如果用户输入的url没有http或www呢?谢谢你的描述。我会检查的。我想你可以用任何东西作为分隔符。标准是/但您必须避开斜杠,因此不使用/此处使其更易于阅读。请检查编辑。如果答案对您的问题有帮助,请接受。谢谢您的回答,但是如果用户输入的url没有http或www,该怎么办?谢谢您的描述。我会检查的。我想你可以用任何东西作为分隔符。标准是/但您必须避开斜杠,因此不使用/此处使其更易于阅读。请检查编辑。如果答案对你的问题有帮助,请接受。