When processing video links, especially platforms like YouTube, extracting the unique identifier (video ID) of a video is a common requirement in many application scenarios. For example, retaining only the video ID is more efficient and consistent when you want to embed a user-submitted YouTube link into a web page or for saving it to a database. In PHP, we can use the built-in parse_url function to implement this function.
This article will introduce how to extract video ID from YouTube links by combining parse_url with other functions.
In practical applications, YouTube's link format may have the following common forms:
Standard link:
https://www.youtube.com/watch?v=VIDEO_ID
Short link:
https://youtu.be/VIDEO_ID
Embed link:
https://www.youtube.com/embed/VIDEO_ID
The parse_url function can decompose a URL into multiple parts, such as scheme, host, path, query, etc. Using this feature, we can easily analyze YouTube links.
function extractYouTubeID($url) {
$parsedUrl = parse_url($url);
// Standard link https://www.youtube.com/watch?v=VIDEO_ID
if (isset($parsedUrl['host']) && strpos($parsedUrl['host'], 'youtube.com') !== false) {
if (isset($parsedUrl['path']) && $parsedUrl['path'] === '/watch') {
if (isset($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $queryParams);
if (isset($queryParams['v'])) {
return $queryParams['v'];
}
}
}
// Embed link https://www.youtube.com/embed/VIDEO_ID
if (strpos($parsedUrl['path'], '/embed/') === 0) {
return substr($parsedUrl['path'], strlen('/embed/'));
}
}
// Short link https://youtu.be/VIDEO_ID
if (isset($parsedUrl['host']) && $parsedUrl['host'] === 'youtu.be') {
return ltrim($parsedUrl['path'], '/');
}
return null; // Unrecognized format
}
$links = [
'https://www.youtube.com/watch?v=abc123XYZ',
'https://youtu.be/abc123XYZ',
'https://www.youtube.com/embed/abc123XYZ',
'https://gitbox.net/watch?v=abc123XYZ', // No YouTube domain name,Should return null
];
foreach ($links as $link) {
$videoId = extractYouTubeID($link);
echo "Link:$link\n";
echo "video ID:" . ($videoId ?? 'Not identified') . "\n\n";
}
The above code has considered three common link forms, but in actual use, you may also encounter different parameter orders and timestamp parameters attached. In order to improve robustness, more verifications can be performed on the parameters after parse_str .
Be especially careful when replacing domain names or identifying non-YouTube links to ensure that only trusted sources are processed.
Through built-in functions such as parse_url and parse_str , PHP can efficiently parse URLs and extract specific parameters. This method is not only simple, but also has good maintainability. Encapsulate it into a general function, which can be reused in various projects and improve development efficiency.