3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @param string $string The string from which the diacritic marks (¨, ´, ~, `, ^, etc.) will be removed. * @return string String without diacritic marks. */ function removeDiacritics($string) { return transliterator_transliterate('Any-Latin;Latin-ASCII', $string); } /** * Ensures that $string is UTF-8 encoded. * @param string $string The string to ensure encoding * @return string The UTF-8 encoded string * @throws Exception If $string has no valid encoding */ function ensureUTF8Encoding($string) { $encoding = mb_detect_encoding($string, 'UTF-8', true); if ($encoding === false) { $encoding = mb_detect_encoding($string, 'auto'); } if ($encoding === false) { throw new Exception('No valid encoding detected'); } return $encoding !== 'UTF-8' ? mb_convert_encoding($string, 'UTF-8', $encoding) : $string; } /** * @param string $string The string to get lower-cased * @return string Lower-cased and UTF-8 encoded value of $string */ function lowerCase($string) { return mb_strtolower(ensureUTF8Encoding($string), 'UTF-8'); } /** * Performs a words comparision between $s1 and $s2. This function counts every word only once, so 'hello hello' * would be 100% similar to 'hello'. Both strings will get converted to lowercase, with its diacritical marks * (´, ¨, ^, `, ~, etc.) stripped out. This behaviour is very much like a 'keywords search'. * @param string $s1 First string to compare * @param string $s2 Second string to compare * @param null|string[] $skipWords Value-only array of words that should'nt be taken into account when comparing $s1 and $s2 * @param null|string[] $skipText Value-only array of text that should'nt be taken into account when comparing $s1 and $s2. * Please note that this text will be stripped from the end, start and middle parts of words. * @return float|int Percent of similarity between $s1 and $s2, where 1 represents 100% and 0 represents 0% */ function compareWords($s1, $s2, $skipWords = [ 'en', 'de', 'del', 'los', 'la', 'in', 'from', 'the' ], $skipText = [ '.', ',', ';', ':' ]) { if ($s1 === null || $s2 === null) return 0; if ($skipText !== null && count($skipText) > 0) { $s1 = str_replace($skipText, '', $s1); $s2 = str_replace($skipText, '', $s2); } if ($skipWords !== null && count($skipWords) > 0) { $skipWords = array_map(function ($item) { return preg_quote($item, '/'); }, $skipWords); $skipWordsRegex = '/(?:(?<=\s)|^)(?:' . implode('|', $skipWords) . ')(?:(?=\s)|$)/'; $s1 = preg_replace($skipWordsRegex, '', $s1); $s2 = preg_replace($skipWordsRegex, '', $s2); } $s1 = trim(lowerCase(preg_replace('/\s+/', ' ', removeDiacritics($s1)))); $s2 = trim(lowerCase(preg_replace('/\s+/', ' ', removeDiacritics($s2)))); if (strlen($s1) === 0 || strlen($s2) === 0) return 0; $s1Words = array_unique(explode(' ', $s1)); $s2Words = array_unique(explode(' ', $s2)); $s1WordsCount = count($s1Words); $s2WordsCount = count($s2Words); // make sure $s1Words is the smaller array, to have a smaller cycle if ($s1WordsCount > $s2WordsCount) { $temp = $s1Words; $s1Words = $s2Words; $s2Words = $temp; } $s2Words = array_flip($s2Words); $maxWords = max($s1WordsCount, $s2WordsCount); $matches = 0; foreach ($s1Words as $s1Word) { if (array_key_exists($s1Word, $s2Words)) $matches++; } return $matches / $maxWords; } var_dump(compareWords('The tomato sauce.', 'tomato sauce'));

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0060.04522.08
8.3.50.0100.05321.69
8.3.40.0040.02822.46
8.3.30.0140.01722.59
8.3.20.0070.01123.56
8.3.10.0030.01327.28
8.3.00.0040.01423.55
8.2.180.0100.04822.22
8.2.170.0070.02422.96
8.2.160.0030.02723.71
8.2.150.0070.01024.18
8.2.140.0100.00724.66
8.2.130.0000.01626.16
8.2.120.0060.01025.74
8.2.110.0070.01123.02
8.2.100.0000.02521.84
8.2.90.0060.01622.55
8.2.80.0030.01721.75
8.2.70.0030.01721.80
8.2.60.0120.01521.55
8.2.50.0000.02022.03
8.2.40.0070.01325.90
8.2.30.0030.01622.96
8.2.20.0030.01621.81
8.2.10.0070.01321.82
8.2.00.0000.01922.07
8.1.270.0070.01025.81
8.1.260.0030.01326.35
8.1.250.0040.01228.09
8.1.240.0080.01223.20
8.1.230.0070.01623.09
8.1.220.0030.01621.41
8.1.210.0040.01821.36
8.1.200.0030.01721.38
8.1.190.0070.01321.10
8.1.180.0030.01721.38
8.1.170.0030.01722.59
8.1.160.0030.01622.58
8.1.150.0000.02023.84
8.1.140.0030.01621.60
8.1.130.0040.01522.58
8.1.120.0040.01521.61
8.1.110.0060.01421.52
8.1.100.0100.01021.48
8.1.90.0030.01921.39
8.1.80.0030.02321.50
8.1.70.0090.01221.46
8.1.60.0000.02121.67
8.1.50.0030.01721.52
8.1.40.0030.01621.58
8.1.30.0040.01821.80
8.1.20.0060.01521.73
8.1.10.0030.01621.68
8.1.00.0000.02121.60
8.0.300.0000.02021.77
8.0.290.0000.02120.75
8.0.280.0000.01922.00
8.0.270.0030.01621.05
8.0.260.0120.01222.08
8.0.250.0070.01120.82
8.0.240.0030.01620.80
8.0.230.0090.00920.87
8.0.220.0040.02220.86
8.0.210.0060.01320.73
8.0.200.0030.01620.98
8.0.190.0000.02020.96
8.0.180.0070.01420.80
8.0.170.0110.01120.79
8.0.160.0040.01620.96
8.0.150.0090.01220.74
8.0.140.0070.01420.82
8.0.130.0000.00713.57
8.0.120.0070.01420.79
8.0.110.0030.01720.93
8.0.100.0000.02020.88
8.0.90.0030.01720.89
8.0.80.0130.02421.00
8.0.70.0070.01420.77
8.0.60.0030.01921.05
8.0.50.0030.01720.92
8.0.30.0100.02521.41
8.0.20.0130.02821.12
8.0.10.0060.01320.95
8.0.00.0100.03120.55
7.4.330.0020.00215.55
7.4.320.0040.01420.66
7.4.300.0000.01920.50
7.4.290.0030.01620.60
7.4.280.0030.01720.62
7.4.270.0030.01620.64
7.4.260.0000.02020.61
7.4.250.0000.02220.52
7.4.240.0000.01920.58
7.4.230.0000.01920.75
7.4.220.0030.01620.66
7.4.210.0160.02020.66
7.4.200.0060.01220.69
7.4.160.0120.02620.39
7.4.150.0130.03220.42
7.4.140.0160.03220.49
7.4.130.0230.03720.63
7.4.120.0140.03220.51
7.4.110.0190.01920.61
7.4.100.0130.02620.48
7.4.90.0160.02520.58
7.4.80.0060.03520.56
7.4.70.0130.04420.78
7.4.60.0160.04820.58
7.4.50.0040.02620.61
7.4.40.0110.02920.50
7.4.00.0000.03318.99
7.3.330.0000.00513.64
7.3.320.0000.00613.60
7.3.310.0030.01620.30
7.3.300.0060.01220.53
7.3.290.0120.03420.46
7.3.280.0140.03220.40
7.3.270.0100.03020.69
7.3.260.0150.03220.56
7.3.240.0110.02920.48
7.3.230.0120.02520.60
7.3.210.0130.03720.64
7.3.200.0160.02620.47
7.3.190.0060.03320.70
7.3.180.0180.02120.75
7.3.170.0060.03220.52
7.3.160.0060.03420.59
7.3.120.0100.02019.11
7.3.110.0000.02719.11
7.3.100.0090.01318.84
7.3.90.0070.02018.80
7.3.80.0040.02218.83
7.3.70.0030.02119.00
7.3.60.0060.02318.83
7.3.50.0070.02119.09
7.3.40.0070.02018.75
7.3.30.0000.02419.10
7.3.20.0130.01020.67
7.3.10.0080.02220.74
7.3.00.0080.02120.59
7.2.330.0120.02920.80
7.2.320.0100.03020.88
7.2.310.0140.02720.72
7.2.300.0180.03120.83
7.2.290.0160.04220.72
7.2.240.0100.01719.05
7.2.230.0030.02319.28
7.2.220.0070.01819.20
7.2.210.0040.02219.14
7.2.200.0150.01219.27
7.2.190.0000.02319.06
7.2.180.0040.02519.18
7.2.170.0060.02519.09
7.2.160.0070.02019.09
7.2.150.0000.03120.96
7.2.140.0070.02220.96
7.2.130.0070.02120.74
7.2.120.0050.02320.69
7.2.110.0080.02220.81
7.2.100.0050.02320.90
7.2.90.0100.01920.79
7.2.80.0030.02420.77
7.2.70.0060.02220.81
7.2.60.0080.01920.80
7.2.50.0100.01920.94
7.2.40.0090.02020.87
7.2.30.0090.01920.95
7.2.20.0060.02721.50
7.2.10.0090.02521.40
7.2.00.0080.02621.44
7.1.330.0000.02419.36
7.1.320.0070.02019.49
7.1.310.0000.02419.50
7.1.300.0070.02719.51
7.1.290.0030.02419.84
7.1.280.0060.01919.68
7.1.270.0090.01819.70
7.1.260.0000.02119.67
7.1.250.0070.02219.76
7.1.200.0030.02319.61
7.1.140.0100.04522.02
7.1.130.0100.03122.34
7.1.120.0090.03822.00
7.1.110.0060.03522.62
7.1.100.0100.03322.38
7.1.90.0150.04422.33
7.1.80.0150.02822.33
7.1.70.0090.03521.63
7.1.60.0260.03639.32
7.1.50.0280.04238.75
7.1.40.0320.05438.48
7.1.30.0160.04438.44
7.1.20.0320.03238.54
7.1.10.0080.03220.78
7.1.00.0070.03920.69

preferences:
64.12 ms | 400 KiB | 5 Q