<?php
$testStrings = [
"H3||0",
"Hallo",
"aloha",
"h",
"hallo",
"hallå",
"hel",
"helicopter",
"hellacious",
"hello",
"hello y'all",
"hello yall",
"helów",
"hey hello",
"hola",
"hêllo",
"mellow yellow",
"try",
];
$needle = 'hello';
usort($testStrings, fn($a, $b) =>
levenshtein($needle, $a) <=> levenshtein($needle, $b)
?: similar_text($needle, $b) <=> similar_text($needle, $a)
);
var_export($testStrings);
- Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- array (
0 => 'hello',
1 => 'hallo',
2 => 'hêllo',
3 => 'Hallo',
4 => 'hel',
5 => 'hallå',
6 => 'helów',
7 => 'hola',
8 => 'hey hello',
9 => 'h',
10 => 'hellacious',
11 => 'hello yall',
12 => 'aloha',
13 => 'H3||0',
14 => 'try',
15 => 'hello y\'all',
16 => 'helicopter',
17 => 'mellow yellow',
)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
array (
0 => 'hello',
1 => 'hallo',
2 => 'hêllo',
3 => 'Hallo',
4 => 'hel',
5 => 'hallå',
6 => 'helów',
7 => 'hola',
8 => 'hey hello',
9 => 'h',
10 => 'hellacious',
11 => 'hello yall',
12 => 'aloha',
13 => 'H3||0',
14 => 'try',
15 => 'hello y\'all',
16 => 'helicopter',
17 => 'mellow yellow',
)
- Output for 7.4.0 - 7.4.33
- array (
0 => 'hello',
1 => 'hallo',
2 => 'hêllo',
3 => 'hel',
4 => 'Hallo',
5 => 'hallå',
6 => 'helów',
7 => 'hola',
8 => 'hey hello',
9 => 'h',
10 => 'hellacious',
11 => 'hello yall',
12 => 'aloha',
13 => 'H3||0',
14 => 'try',
15 => 'hello y\'all',
16 => 'helicopter',
17 => 'mellow yellow',
)
- Output for 7.3.0 - 7.3.33
- Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ')' in /in/aNuKt on line 26
Process exited with code 255.
preferences:
188.08 ms | 401 KiB | 156 Q