-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestMultiRedirections.php
65 lines (58 loc) · 1.86 KB
/
testMultiRedirections.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
$patterns = ['toto', 'ta*', 'tititi', 'tata*'];
$destinations = ['tutu', 'tata', 'titi'];
$repeatedPatterns = [];
$repeatedDestinations = [];
for ($i = 0; $i < 1000; $i++) {
foreach ($patterns as $pattern) {
$repeatedPatterns[] = $pattern;
}
foreach ($destinations as $destination) {
$repeatedDestinations[] = $destination.$i;
}
}
function testPatternsOnPatterns($patterns)
{
if (2 > count($patterns)) {
return [];
}
$uselessPatterns = [];
for ($i = 0; $i < count($patterns); $i++) {
$pattern = $patterns[$i];
if (strlen($pattern) - 1 === strrpos($pattern, '*')) {
$pattern = substr($pattern, 0, -1).'.*';
}
$pattern = '/^'.$pattern.'$/';
for ($j = $i + 1; $j < count($patterns); $j++) {
$testedPattern = $patterns[$j];
if (0 < preg_match($pattern, $testedPattern)) {
$uselessPatterns[] = array_splice($patterns, $j, 1)[0];
}
}
}
return $uselessPatterns;
}
function testPatternsOnDestinations($patterns, $destinations)
{
$redirectedDestinations = [];
foreach ($patterns as $pattern) {
if (strlen($pattern) - 1 === strrpos($pattern, '*')) {
$pattern = substr($pattern, 0, -1).'.*';
}
$pattern = '/^'.$pattern.'$/';
foreach ($destinations as $destination) {
if (0 < preg_match($pattern, $destination)) {
$redirectedDestinations[] = $destination;
}
}
}
return $redirectedDestinations;
}
$start = microtime(true);
$redirectedDestinations = testPatternsOnDestinations($repeatedPatterns, $repeatedDestinations);
//$uselessPatterns = testPatternsOnPatterns($repeatedPatterns);
$stop = microtime(true);
$time = $stop - $start;
echo 'time: '.$time."\n";
//var_dump($redirectedDestinations);
//var_dump($uselessPatterns);