Home >Backend Development >PHP Tutorial >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:
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-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|</a>))[?=&+%\w.-]*
Regex Explanation
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. [?=&+%\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=">YouTube link: </a>', $text); return $text; }
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-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|</a>))[?=&+%\w.-]*/ig; return text.replace(re, '<a href="http://www.youtube.com/watch?v=">YouTube link: </a>'); }
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!