Signature
metaphone(string $string, int $max_phonemes = 0): stringPurpose
metaphone() converts a string into a metaphone key, a compact code that approximates how an English word sounds. Words that sound alike tend to produce the same or similar keys, even when their spelling differs. That makes the function useful for fuzzy matching of names, search suggestions and duplicate detection.
The manual describes it as similar to soundex() but more accurate, because it applies basic rules of English pronunciation. Unlike a soundex code, a metaphone key has variable length. The algorithm was created by Lawrence Philips.
Signature
Parameters and defaults
$stringis the input text whose key you want.$max_phonemeslimits the length of the returned key. The default0means no limit. A phoneme is always written out completely, so the result can be slightly longer than the limit you asked for.
Return value and failure cases
The function returns the metaphone key as a string. Before PHP 8.0.0 it could return false on failure; in PHP 8.4 the return type is always string, so you do not need to check for false. Pass a non-negative $max_phonemes; a negative limit is not meaningful.
Example 1: Comparing two words
The words programming and programmer share a stem but end differently. Their keys share the same beginning and differ at the end.
$keys = [metaphone('programming'), metaphone('programmer')];
echo json_encode($keys, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";["PRKRMNK","PRKRMR"]
Example 2: Limiting the key with max_phonemes
With a limit of 5, both words are cut to the same key. That is handy when you only want to compare the start of a word.
$keys = [
'programming' => metaphone('programming', 5),
'programmer' => metaphone('programmer', 5),
];
echo json_encode($keys, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"programming":"PRKRM","programmer":"PRKRM"}
Example 3: A limit can be exceeded
The letter x is transcribed as the two characters KS. Because a phoneme is never split, asking for five characters can give you six.
$key = metaphone('Asterix', 5);
$result = ['key' => $key, 'length' => strlen($key)];
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";{"key":"ASTRKS","length":6}
Edge cases
- Overshooting the limit. As Example 3 shows,
$max_phonemesis a soft limit. Do not use it to guarantee a fixed column width or array key length. If you need an exact size, trim the key yourself withsubstr()after the call, accepting that you may cut a two-letter phoneme in half. - Variable length. Keys are not padded. Two different words can have keys of different lengths, so compare keys with strict string equality rather than assuming a fixed format.
- Different words, same key. The key is an approximation. Distinct words can collide, and words that sound alike to a human can still produce different keys.
- English focus. The rules model English pronunciation. Names and words from other languages may produce keys that do not match how they are really spoken.
- Case. The key is built from letters regardless of how you capitalise the input, and the result is uppercase, so you do not need to normalise case before storing keys.
Common mistakes
- Treating the key as unique. A metaphone key is a bucket, not an identifier. Use it to find candidates, then confirm with another check.
- Expecting
max_phonemesto be exact. Plan for results a character longer than the limit. - Checking for
false. Old code written for PHP 7 often tests forfalse. In PHP 8 and later the function always returns a string. - Mixing algorithms. Keys from
metaphone()cannot be compared with codes fromsoundex(). Pick one function and use it consistently for both stored and searched values. - Using it for non-English text. For multilingual matching, a phonetic key for English will be unreliable.
Related functions
soundex()creates a simpler, fixed-style phonetic code and is generally considered less accurate.levenshtein()counts the edits needed to turn one string into another, which complements phonetic matching.similar_text()measures how similar two strings are based on shared characters.
A common pattern is to use metaphone() to narrow a list of candidates and then rank them with levenshtein().
Recap
metaphone() takes a string and an optional $max_phonemes limit and returns an uppercase phonetic key for English-sounding words. The key length varies, the limit is soft because whole phonemes are kept, and the return type is always string in PHP 8.4. Use it to group similar-sounding words, and confirm matches with another comparison.
Tested examples
$keys = [metaphone('programming'), metaphone('programmer')];
echo json_encode($keys, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
["PRKRMNK","PRKRMR"]
$keys = [
'programming' => metaphone('programming', 5),
'programmer' => metaphone('programmer', 5),
];
echo json_encode($keys, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"programming":"PRKRM","programmer":"PRKRM"}
$key = metaphone('Asterix', 5);
$result = ['key' => $key, 'length' => strlen($key)];
echo json_encode($result, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), "\n";
{"key":"ASTRKS","length":6}