The soundex Function
The soundex
function calculates the sound key for a given string.
The soundex key consists of the first letter of the string followed by three digits,
representing the main sound characteristics of the rest of the string.
This algorithm is particularly useful for searching names that sound similar,
but may be spelled differently.
Syntax
soundex(string);
Example
Get the soundex key for the string "Hello"
:
<?php
echo soundex('Hello');
?>
Execution result:
'H400'
Example
Compare soundex keys for similarly sounding words:
<?php
$res1 = soundex('Robert');
$res2 = soundex('Rupert');
echo $res1 . ' ' . $res2;
?>
Execution result:
'R163 R163'
Example
Check soundex keys for different words:
<?php
$words = ['Hello', 'Hallo', 'Hullo', 'World'];
foreach ($words as $word) {
echo $word . ': ' . soundex($word) . "\n";
}
?>
Execution result:
Hello: H400
Hallo: H400
Hullo: H400
World: W643
See Also
-
the
levenshtein
function,
which calculates the distance between strings -
the
metaphone
function,
which returns the metaphone key of a string