Skip to content

Instantly share code, notes, and snippets.

@richaber
Forked from eyecatchup/ParseYoutubeUrlTest.php
Created April 20, 2022 17:28
Show Gist options
  • Save richaber/13ac2678bb73cb5edbcbec1ae8a7c59b to your computer and use it in GitHub Desktop.
Save richaber/13ac2678bb73cb5edbcbec1ae8a7c59b to your computer and use it in GitHub Desktop.
<?php
// Function `parse_yturl()` from <http://stackoverflow.com/a/10524505/624466>
/**
* Check if the input string is a valid YouTube URL
* and try to extract the YouTube Video ID from it.
*
* @author Stephan Schmitz <eyecatchup@gmail.com>
* @param $url string The string that shall be checked.
* @return mixed YouTube Video ID, or (boolean) false.
*/
function parse_yturl($url) {
$pattern = '#^(?:https?://|//)?' # Optional URL scheme. Either http, or https, or protocol-relative.
. '(?:www\.|m\.)?' # Optional www or m subdomain.
. '(?:' # Group host alternatives:
. 'youtu\.be/' # Either youtu.be,
. '|youtube\.com/' # or youtube.com
. '(?:' # Group path alternatives:
. 'embed/' # Either /embed/,
. '|v/' # or /v/,
. '|watch\?v=' # or /watch?v=,
. '|watch\?.+&v=' # or /watch?other_param&v=
. ')' # End path alternatives.
. ')' # End host alternatives.
. '([\w-]{11})' # 11 characters (Length of Youtube video ids).
. '(?![\w-])#'; # Rejects if overlong id.
preg_match($pattern, $url, $matches);
return (isset($matches[1])) ? $matches[1] : false;
}
// Tests for function `parse_yturl()`
$testIds = [
'l8qdouU9cYE',
'_PqP1qHhsyM',
'3G3_im4RyI8',
'uqU4Mkd-_gU',
];
$testUrls = [
// Without scheme and subdomain (Domain: youtu.be, Path: /)
'youtu.be/%s',
// Without scheme, with subdomain (Domain: youtu.be, Path: /)
'www.youtu.be/%s',
// With HTTP scheme, without subdomain (Domain: youtu.be, Path: /)
'http://youtu.be/%s',
// With HTTP scheme and subdomain (Domain: youtu.be, Path: /)
'http://www.youtu.be/%s',
// With HTTPS scheme, without subdomain (Domain: youtu.be, Path: /)
'https://youtu.be/%s',
// With HTTPS scheme and subdomain (Domain: youtu.be, Path: /)
'https://www.youtu.be/%s',
// Without scheme and subdomain (Domain: youtube.com, Path: /embed)
'youtube.com/embed/%s',
'youtube.com/embed/%s&other_params',
// Without scheme, with subdomain (Domain: youtube.com, Path: /embed)
'www.youtube.com/embed/%s',
'www.youtube.com/embed/%s&other_params',
// With HTTP scheme, without subdomain (Domain: youtube.com, Path: /embed)
'http://youtube.com/embed/%s',
'http://youtube.com/embed/%s&other_params',
// With HTTP scheme and subdomain (Domain: youtube.com, Path: /embed)
'http://www.youtube.com/embed/%s',
'http://www.youtube.com/embed/%s&other_params',
// With HTTPS scheme, without subdomain (Domain: youtube.com, Path: /embed)
'https://youtube.com/embed/%s',
'https://youtube.com/embed/%s&other_params',
// With HTTPS scheme and subdomain (Domain: youtube.com, Path: /embed)
'https://www.youtube.com/embed/%s',
'https://www.youtube.com/embed/%s&other_params',
// Without scheme and subdomain (Domain: youtube.com, Path: /v)
'youtube.com/v/%s',
'youtube.com/v/%s&other_params',
// Without scheme, with subdomain (Domain: youtube.com, Path: /v)
'www.youtube.com/v/%s',
'www.youtube.com/v/%s&other_params',
// With HTTP scheme, without subdomain (Domain: youtube.com, Path: /v)
'http://youtube.com/v/%s',
'http://youtube.com/v/%s&other_params',
// With HTTP scheme and subdomain (Domain: youtube.com, Path: /v)
'http://www.youtube.com/v/%s',
'http://www.youtube.com/v/%s&other_params',
// With HTTPS scheme, without subdomain (Domain: youtube.com, Path: /v)
'https://youtube.com/v/%s',
'https://youtube.com/v/%s&other_params',
// With HTTPS scheme and subdomain (Domain: youtube.com, Path: /v)
'https://www.youtube.com/v/%s',
'https://www.youtube.com/v/%s&other_params',
// Without scheme and subdomain (Domain: youtube.com, Path: /watch)
'youtube.com/watch?v=%s',
'youtube.com/watch?v=%s&other_params',
'youtube.com/watch?other_params&v=%s',
'youtube.com/watch?other_params&v=%s&more_params',
// Without scheme, with subdomain (Domain: youtube.com, Path: /watch)
'www.youtube.com/watch?v=%s',
'www.youtube.com/watch?v=%s&other_params',
'www.youtube.com/watch?other_params&v=%s',
'www.youtube.com/watch?other_params&v=%s&more_params',
// With HTTP scheme, without subdomain (Domain: youtube.com, Path: /watch)
'http://youtube.com/watch?v=%s',
'http://youtube.com/watch?v=%s&other_params',
'http://youtube.com/watch?other_params&v=%s',
'http://youtube.com/watch?other_params&v=%s&more_params',
// With HTTP scheme and subdomain (Domain: youtube.com, Path: /watch)
'http://www.youtube.com/watch?v=%s',
'http://www.youtube.com/watch?v=%s&other_params',
'http://www.youtube.com/watch?other_params&v=%s',
'http://www.youtube.com/watch?other_params&v=%s&more_params',
// With HTTPS scheme, without subdomain (Domain: youtube.com, Path: /watch)
'https://youtube.com/watch?v=%s',
'https://youtube.com/watch?v=%s&other_params',
'https://youtube.com/watch?other_params&v=%s',
'https://youtube.com/watch?other_params&v=%s&more_params',
// With HTTPS scheme and subdomain (Domain: youtube.com, Path: /watch)
'https://www.youtube.com/watch?v=%s',
'https://www.youtube.com/watch?v=%s&other_params',
'https://www.youtube.com/watch?other_params&v=%s',
'https://www.youtube.com/watch?other_params&v=%s&more_params',
];
$results = [];
foreach ($testIds as $id) {
$results[$id] = [];
foreach ($testUrls as $str) {
$testString = sprintf($str, $id);
array_push($results[$id], [
'str' => $testString,
'id' => parse_yturl($testString),
]);
}
}
$matches = 0;
$errors = 0;
//header('Content-type: text/plain;');
foreach ($results as $key => $val) {
$testId = $key;
printf('# Testing strings for id "%s"' . PHP_EOL . PHP_EOL, $testId);
foreach ($val as $res) {
if ($res['id'] !== $testId) {
$errors++;
$result = 'no match';
} else {
$matches++;
$result = $res['id'];
}
printf(' Result: %s, String: %s' . PHP_EOL, $result, $res['str']);
}
print PHP_EOL;
}
printf('Done. %d assertation(s), %d error(s)' . PHP_EOL, $matches, $errors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment