metaphone

Calculates a phonetic key for an English word so similar-sounding words compare as equal.

Signature
metaphone(string $string, int $max_phonemes = 0): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe input word or phrase to transcribe.
$max_phonemesoptionalint= 0Truncates the key to roughly this many characters. Phonemes are always transcribed completely, so the result may be slightly longer. 0 means no limit.

Return value

stringThe metaphone key for the input string. The key is variable length and consists of uppercase consonant symbols (plus '0' for the 'th' sound).

Errors

Throws a ValueError if max_phonemes is negative.

Changelog

  • PHP 8.0The function no longer returns false on failure; it always returns a string.

metaphone turns a word into a rough phonetic fingerprint. Two spellings that an English speaker would pronounce the same way usually produce the same key, so you can compare keys instead of comparing letters.

When to reach for it

Use it for fuzzy lookup on human-entered text: matching a typed surname against a customer table, catching a misspelled city, or grouping product names that people spell inconsistently. The usual pattern is to store the key in an indexed column at write time and search on the key, because computing it for every row at query time does not scale.

What trips people up

The algorithm encodes English pronunciation rules only. Accented letters and non-Latin scripts are not folded, so Plaçe and Place give different keys — normalise or transliterate first. It is also lossy in the other direction: short keys collide freely, so a matching key means "worth checking", not "same word". In practice you filter candidates by metaphone key and then rank them with similar_text or levenshtein.

Compared to soundex, metaphone is more accurate and produces variable-length keys rather than a fixed four characters, which makes it the better default for name matching.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Match a customer-typed surname against stored names when the spelling is uncertain
  • Build a "did you mean" suggestion list for a product search box
  • Group duplicate contact records that differ only in spelling
  • Deduplicate imported CSV rows where names were transcribed by hand

Frequently asked questions

What is the difference between metaphone and soundex?

metaphone applies English pronunciation rules and returns a variable-length key, while soundex returns a fixed four-character code based on a simpler letter-mapping table. metaphone produces fewer false matches, so it is the better choice for name matching; soundex is faster and more predictable in length.

Does metaphone work with accented or non-English characters?

No. It ignores characters outside the English alphabet, so "Plaçe" and "Place" yield different keys. Transliterate the input to plain ASCII first, for example with iconv or a normalisation step, before generating the key.

Can two different words return the same metaphone key?

Yes, and often. The key is a lossy phonetic summary, so a match means the words plausibly sound alike, not that they are the same. Use it to narrow candidates, then rank them with similar_text or levenshtein.

More examples

Limiting the key length

The second argument caps the key so that only the leading sounds are compared. A shorter key matches more loosely: with the full key only close spellings match, while a 2-character key groups everything starting with the same sound. Note that the result can be one character longer than the limit, because a phoneme is never cut in half.

Example · php
<?php
$name = 'Christopher';

echo metaphone($name), "\n";
echo metaphone($name, 4), "\n";
echo metaphone($name, 2), "\n";

Fuzzy lookup over a small list

The misspelled query is reduced to a key and every directory entry with the same key is returned. All three Katherine spellings share a key while Kevin does not, so the filter keeps the plausible candidates and drops the unrelated name. Note that array_filter preserves keys, hence the array_values() wrapper.

Example · php
<?php
$directory = ['Katherine', 'Catherine', 'Kathryn', 'Kevin'];
$query = 'Kathrine';
$key = metaphone($query);

$hits = array_values(array_filter(
    $directory,
    fn (string $n): bool => metaphone($n) === $key
));

print_r($hits);

Discussion

  • Be the first to comment on this lesson.