In PHP, the metaphone function encodes a word into a phonetic code which represents how that word sounds in English. Words which are spelled differently but pronounced alike receive the same code, which makes the function useful for fuzzy search and for matching names. The implementation in the PHP core is the original algorithm published in 1990, and it follows English pronunciation rules only.
Since PHP 8.6, the metaphone function is deprecated. Phonetic algorithms are language-specific and change over time, so they fit better in userland code than in the PHP core. No replacement was added.
<?php
$code1 = metaphone('Smith');
$code2 = metaphone('Smyth');
echo $code1.PHP_EOL;
echo $code2.PHP_EOL;
var_dump($code1 === $code2);
Output:
Deprecated: Function metaphone() is deprecated since 8.6, use a userland phonetic matching library instead in main.php on line 3
Deprecated: Function metaphone() is deprecated since 8.6, use a userland phonetic matching library instead in main.php on line 4
SM0
SM0
bool(true)
Both spellings produce the code SM0, so the comparison returns true. The deprecation is raised at every call.
The soundex function is not deprecated and covers the same kind of matching:
<?php
$code1 = soundex('Smith');
$code2 = soundex('Smyth');
echo $code1.PHP_EOL;
echo $code2.PHP_EOL;
var_dump($code1 === $code2);
Output:
S530
S530
bool(true)
The two functions are not identical. The codes use a different format, SM0 against S530, so phonetic codes which are stored in a database have to be generated again after the change. The soundex algorithm is also less accurate, because it keeps the first letter of the word unchanged. For example, metaphone returns K0RN for both Catherine and Katherine, while soundex returns C365 and K365, so those names stop matching.
For better accuracy, consider finding a phonetic matching library on Packagist and installing it via Composer.
Leave a Comment
Cancel reply