<?php
$examples = [
['https://stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', false],
['http://stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', false],
['//stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', false],
['/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', true],
['/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', true],
['/stackoverflow.com/questions/20248666/in-this-case-which-is-better-in-matching-regular-expression-or-parse-url', true],
];
function is_relative_parse_url($url) {
return parse_url($url, PHP_URL_SCHEME) === null && strncmp($url, '//', 2) !== 0;
}
function is_relative_preg_match_ci($url) {
return preg_match('/^(http|https)?:?\/\//i', $url) === 0;
}
function is_relative_preg_match($url) {
return preg_match('/^(http|https)?:?\/\//', $url) === 0;
}
function is_relative_strpos($url) {
return !(strpos($url, 'https://') === 0
|| strpos($url, 'http://') === 0
|| strpos($url, '//') === 0);
}
function is_relative_str_starts_with_fallback($url) {
if(PHP_MAJOR_VERSION <= 8) {
return is_relative_strpos($url);
}
return !(str_starts_with($url, 'https://')
|| str_starts_with($url, 'http://')
|| str_starts_with($url, '//'));
}
function is_relative_strncmp($url) {
return !(strncmp($url, 'https://', 8)
|| strncmp($url, 'http://', 7)
|| strncmp($url, '//', 2));
}
function is_relative_substr_compare($url) {
return !(
substr_compare($url, 'https://', 0, 8) === 0
|| substr_compare($url, 'http://', 0, 7) === 0
|| substr_compare($url, '//', 0, 2) === 0
);
}
function is_relative_substr_compare_cs($url) {
return !(
substr_compare($url, 'https://', 0, 8, true) === 0
|| substr_compare($url, 'http://', 0, 7, true) === 0
|| substr_compare($url, '//', 0, 2) === 0
);
}
function test($name, $test, $examples, $reps = 200000) {
$time = microtime(true);
for($i = 0; $i < $reps; $i++) {
foreach($examples as [$url, $expected]) {
if ($test($url) !== $expected) {
throw new Exception("Failed test $name with URL $url, expected " . ($expected ? "true": "false"));
}
}
}
echo "$name: ", number_format(microtime(true) - $time, 3), "\n";
}
test('preg_match', function($url) { return is_relative_preg_match($url); }, $examples);
test('preg_match (case insensitive)', function($url) { return is_relative_preg_match_ci($url); }, $examples);
test('strpos', function($url) { return is_relative_strpos($url); }, $examples);
test('str_starts_with', function($url) { return is_relative_str_starts_with_fallback($url); }, $examples);
test('substr_compare (case insensitive)', function($url) { return is_relative_substr_compare($url); }, $examples);
test('substr_compare (case sensitive)', function($url) { return is_relative_substr_compare_cs($url); }, $examples);
test('parse_url', function($url) { return is_relative_parse_url($url); }, $examples);
preferences:
29.32 ms | 411 KiB | 5 Q