Home > Backend Development > PHP Tutorial > How Can I Extract YouTube Video IDs from Text Using Regular Expressions?

How Can I Extract YouTube Video IDs from Text Using Regular Expressions?

Mary-Kate Olsen
Release: 2024-12-16 19:19:12
Original
707 people have browsed it

How Can I Extract YouTube Video IDs from Text Using Regular Expressions?

Finding YouTube Video IDs from Text with Regular Expressions

Your goal is to identify all YouTube video URLs and retrieve their corresponding IDs from a text field. Regular expressions offer a powerful tool for accomplishing this task.

Understanding YouTube URL Formats

YouTube URLs come in various forms, including:

  • Short format: https://youtu.be/NLqAF9hrVbY
  • Embed iframe: https://www.youtube.com/embed/NLqAF9hrVbY
  • Watch page: https://www.youtube.com/watch?v=NLqAF9hrVbY

Regex for YouTube Video ID Extraction

Below is a regular expression that matches all these formats and captures the YouTube video ID:

https?://(?:[0-9A-Z-]+\.)?(?:youtu\.be/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&amp;+%\w.-]*(?:['"][^<>]*>|</a>))[?=&amp;+%\w.-]*
Copy after login

Regex Explanation

  • https?://: Matches the scheme part of the URL (HTTP or HTTPS).
  • (?:[0-9A-Z-] .)?: Optional subdomain part (e.g., www.).
  • (?:youtu.be/|youtube(?:-nocookie)?.comS*?1): Matches the YouTube host part. It handles multiple subdomains and formats.
  • ([w-]{11}): Captures the YouTube video ID, which consists of exactly 11 alphanumeric characters and hyphens.
  • (?=2|$): Asserts that the next character after the ID is either a non-word character or the end of the string.
  • (?![?=& %w.-](?:['"]3>|))[?=& %w.-]*: Negative lookahead to exclude pre-linked URLs.

Example Code

In PHP, you can use the following function to replace the video URLs with links:

function linkifyYouTubeURLs($text) {
    $text = preg_replace('~(?#!js YouTubeId Rev:20160125_1800)
        # Match non-linked youtube URL in the wild. (Rev:20130823)
        https?://          # Required scheme. Either http or https.
        (?:[0-9A-Z-]+\.)?  # Optional subdomain.
        (?:                # Group host alternatives.
          youtu\.be/       # Either youtu.be,
        | youtube          # or youtube.com or
          (?:-nocookie)?   # youtube-nocookie.com
          \.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})        # : VIDEO_ID is exactly 11 chars.
        (?=[^\w-]|$)       # Assert next char is non-ID or EOS.
        (?!                # Assert URL is not pre-linked.
          [?=&amp;+%\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.
        [?=&amp;+%\w.-]*       # Consume any URL (query) remainder.
        ~ix', '<a href="http://www.youtube.com/watch?v=">YouTube link: </a>',
        $text);
    return $text;
}
Copy after login

In JavaScript, the following code performs a similar operation:

function linkifyYouTubeURLs(text) {
    var re = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*?[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&amp;+%\w.-]*(?:['"][^<>]*>|</a>))[?=&amp;+%\w.-]*/ig;
    return text.replace(re, '<a href="http://www.youtube.com/watch?v=">YouTube link: </a>');
}
Copy after login

  1. ws-
  2. w-
  3. <>

The above is the detailed content of How Can I Extract YouTube Video IDs from Text Using Regular Expressions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template