I personally tested a few routines to find out if the extension in a url is one of the searched and after some experiments, I came to the conclusion that the fastest method is not to use preg_match and by no means the function explodes!
In fact, the fastest search was using two php functions: strstr , strrchr and strpos .
I tested a simple url and more complex url to see if the results are valid for a more complicated query. I also tested the case if the extension is not in the ones you are looking for.
Test 1 had the following URLs
- – /test/get.test.css
- – /test/get.test.css?param1=test1¶m2=test2.js¶m3=test3?4
Test 2 had the following URLs
- – /test/get.test.csss
- – /test/get.test.csss?param1=test1¶m2=test2.js¶m3=test3?4
The results for 10 million iterations for each method were as follows:
Test 1:
a) simple url, valid extension
1: Time1: 1.9181 Sec 1: Time2: 2.6230 Sec 1: Time3: 1.8428 Sec
b) complex url, valid extension
1: Time1: 2.7627 Sec 1: Time2: 3.0548 Sec 1: Time3: 1.7855 Sec
Test 2:
a) simple url, invalid extension
: Time1: 1.7706 Sec : Time2: 2.7265 Sec : Time3: 1.9815 Sec
b) complex url, ivalid extension
: Time1: 2.1901 Sec : Time2: 3.0742 Sec : Time3: 1.8526 Sec
Where:
- – Time1: preg_match
- – Time2: explode + strpos
- – Time3: strstr & strrchr + strpos
The test shows that using the explode function is not the best solution, it is even much slower than the other two solutions.
Using preg_match or strstr + strrchr is the best option, either is good, although preg_match is 15% slower on average
Below is the php code used for testing:
function byrev_SearchExtInUrl($url, $extensions) { global $mm;
$found = strstr($url, '?', true);
if ($found === false)
$found = &$url;
$found = strrchr($found, '.');
if ($found !== false) {
return (strpos($extensions, $found) !== FALSE);
}
return false;
}
function byrev_SearchExtInUrl2($url, $extensions) {
return (boolean)(preg_match($extensions, $url, $match));
}
function byrev_SearchExtInUrl3($url, $extensions) {
$s = explode('?', $url);
$s = explode('.', $s[0]);
$cs = count($s);
if ($cs>1) {
return (strpos($extensions,$s[$cs-1]) !== FALSE);
}
return false;
}
$url = $_SERVER['REQUEST_URI'];
$ltest = 10000000;
//~~~~ preg_match
$startTime = microtime(true);
$extensions = "/.*\.(?:php|txt|xml|js|css|htm|html)(?:\?\S+)?$/i";
for($i=0; $i<$ltest; $i++ ) {
$Qfound = byrev_SearchExtInUrl2($url, $extensions);
}
$time1 = $Qfound.": Time1: " . number_format(( microtime(true) - $startTime), 4) . " Sec\n";
//~~~~ explode
$startTime = microtime(true);
$Qfound=false;
$extensions = 'php|txt|xml|js|css|htm|html';
for($i=0; $i<$ltest; $i++ ) {
$Qfound = byrev_SearchExtInUrl3($url, $extensions);
}
$time2 = $Qfound.": Time2: " . number_format(( microtime(true) - $startTime), 4) . " Sec\n";
//~~~~ strstr & strrchr
$startTime = microtime(true);
$Qfound = false;
$extensions = '.php|.txt|.xml|.js|.css|.htm|.html';
for($i=0; $i<$ltest; $i++ ) {
$Qfound = byrev_SearchExtInUrl($url, $extensions);
}
$time3 = $Qfound.": Time3: " . number_format(( microtime(true) - $startTime), 4) . " Sec\n";
echo $time1.$time2.$time3;
Be First to Comment