使用正则表达式验证 Youtube URL

2023-10-11php开发问题
28

本文介绍了使用正则表达式验证 Youtube URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试验证我的应用程序的 YouTube 网址.

I'm trying to validate YouTube URLs for my application.

到目前为止,我有以下几点:

So far I have the following:

// Set the youtube URL
$youtube_url = "www.youtube.com/watch?v=vpfzjcCzdtCk";

if (preg_match("/((http://){0,}(www.){0,}(youtube.com){1} || (youtu.be){1}(/watch?v=[^s]){1})/", $youtube_url) == 1)
{
    echo "Valid";
else
{
    echo "Invalid";
}

我希望验证 Youtube 网址的以下变体:

I wish to validate the following variations of Youtube Urls:

  • 有和没有 http://
  • 有和没有 www.
  • 使用网址 youtube.com 和 youtu.be
  • 必须有/watch?v=
  • 必须具有唯一的视频字符串(在上面的示例中为vpfzjcCzdtCk")

但是,我认为我的逻辑不正确,因为出于某种原因它返回 true 用于:www.youtube.co/watch?v=vpfzjcCzdtCk(注意我用 .co 而不是 .com 写错了)

However, I don't think I've got my logic right, because for some reason it returns true for: www.youtube.co/watch?v=vpfzjcCzdtCk (Notice I've written it incorrectly with .co and not .com)

推荐答案

在你的这个正则表达式中有很多冗余(还有 倾斜牙签综合症).不过,这应该会产生结果:

There are a lot of redundancies in this regular expression of yours (and also, the leaning toothpick syndrome). This, though, should produce results:

$rx = '~
  ^(?:https?://)?                           # Optional protocol
   (?:www[.])?                              # Optional sub-domain
   (?:youtube[.]com/watch[?]v=|youtu[.]be/) # Mandatory domain name (w/ query string in .com)
   ([^&]{11})                               # Video id of 11 characters as capture group 1
    ~x';

$has_match = preg_match($rx, $url, $matches);

// if matching succeeded, $matches[1] would contain the video ID

一些注意事项:

  • 使用波浪号 ~ 作为分隔符,避免 LTS
  • 使用 [.] 而不是 . 来提高视觉易读性并避免 LTS.(特殊"字符 - 例如点 . - 在字符类中无效(方括号内))
  • 为了使正则表达式更可读",您可以使用 x 修饰符(这有进一步的含义;请参阅 关于模式修饰符的文档),它还允许在正则表达式中添加注释
  • 可以使用非捕获组来抑制捕获:(?: ).这使表达式更有效.
  • use the tilde character ~ as delimiter, to avoid LTS
  • use [.] instead of . to improve visual legibility and avoid LTS. ("Special" characters - such as the dot . - have no effect in character classes (within square brackets))
  • to make regular expressions more "readable" you can use the x modifier (which has further implications; see the docs on Pattern modifiers), which also allows for comments in regular expressions
  • capturing can be suppressed using non-capturing groups: (?: <pattern> ). This makes the expression more efficient.

或者,要从(或多或少完整的)URL 中提取值,您可能需要使用 parse_url():

Optionally, to extract values from a (more or less complete) URL, you might want to make use of parse_url():

$url = 'http://youtube.com/watch?v=VIDEOID';
$parts = parse_url($url);
print_r($parts);

输出:

Array
(
    [scheme] => http
    [host] => youtube.com
    [path] => /watch
    [query] => v=VIDEOID
)

验证域名和提取视频 ID 留给读者作为练习.

Validating the domain name and extracting the video ID is left as an exercise to the reader.

我屈服于下面的评论战;感谢 Toni Oriol,正则表达式现在也适用于短 (youtu.be) 网址.

I gave in to the comment war below; thanks to Toni Oriol, the regular expression now works on short (youtu.be) URLs as well.

这篇关于使用正则表达式验证 Youtube URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17