W3docs

metaphone()

Our article is about the PHP function metaphone(), which is used to calculate the metaphone key of a string. This function is useful for working with strings in

The PHP metaphone() function calculates the metaphone key of a string — a code that represents how the string sounds in English rather than how it is spelled. Words that are pronounced alike map to the same (or very similar) key, which makes metaphone() useful for fuzzy matching: "Catherine" and "Katherine", or "Smith" and "Smyth", all collapse onto one key.

Deprecated since PHP 8.4.0. metaphone() still works in current PHP, but it is on the path to removal. For new code, prefer a maintained phonetic library (or soundex() for a simpler built-in). The function is documented here because plenty of existing codebases still rely on it.

This article covers the syntax, the optional $phonemes argument, a few worked examples, and where metaphone keys are actually useful.

Syntax

metaphone(string $string, int $phonemes = 0): string|false
ParameterDescription
$stringThe input string to encode.
$phonemesOptional. The maximum number of characters (phonemes) to return. When 0 (the default), the full key is returned.

The return value is the metaphone key as an uppercase string, or false on failure.

Basic example

php— editable, runs on the server

Output:

HLWRLT

Notice that non-letter characters such as ! and the space are ignored, and the result is purely consonant-and-vowel sound codes. The key is not meant to be human-readable — it is a normalized fingerprint of the pronunciation.

How the key encodes sounds

Metaphone applies English pronunciation rules, so the output is not a one-to-one transliteration. A couple of conventions are worth knowing:

  • The digit 0 (zero) stands for the "th" sound.
  • Silent and duplicate letters are dropped, and some letter pairs collapse (for example ph becomes F).
<?php
echo metaphone("Thompson"), "\n"; // 0MPSN  — "Th" → 0, silent p kept by the rule
echo metaphone("Smith"), "\n";    // SM0    — trailing "th" → 0
echo metaphone("PHP"), "\n";      // FP     — "ph" → F
?>

Output:

0MPSN
SM0
FP

Limiting the key length with $phonemes

The second argument caps how many characters the key may contain. This is handy when you only want to compare the first few sounds of long words.

<?php
echo metaphone("Thompson"), "\n";    // full key
echo metaphone("Thompson", 4), "\n"; // first 4 phonemes only
echo metaphone("Wikipedia", 4), "\n";
?>

Output:

0MPSN
0MPS
WKPT

Matching words that sound alike

The real value of metaphone is grouping different spellings of the same sound. Compare two spellings of the same surname:

<?php
$a = metaphone("Catherine");
$b = metaphone("Katherine");

echo $a, "\n";                       // K0RN
echo $b, "\n";                       // K0RN
echo $a === $b ? "Match\n" : "No\n"; // Match
?>

Output:

K0RN
K0RN
Match

Because both names share a metaphone key, a search index keyed on metaphone() would return one when the user typed the other — the basis of "did you mean…?" features, deduplicating contact lists, and tolerant name search.

When to use it (and what to use instead)

Use caseNotes
Fuzzy name / word searchIndex the metaphone key alongside the original value, then match on the key.
Spelling-tolerant lookupsCatches phonetic typos that exact matching misses.
Deduplicating recordsGroup rows whose names sound the same.

metaphone() only models English pronunciation, so it is unreliable for other languages. For typo-distance rather than sound, reach for levenshtein() or similar_text(). For a simpler, non-deprecated phonetic code, soundex() is the built-in alternative.

Summary

metaphone() turns a word into a phonetic key so that differently spelled but similar-sounding words match. Remember that the key encodes English pronunciation (with 0 standing for "th"), that the optional $phonemes argument trims the key length, and that the function is deprecated as of PHP 8.4 — prefer soundex() or a maintained library in new projects.

Practice

Practice
What is the purpose of the metaphone() function in PHP?
What is the purpose of the metaphone() function in PHP?
Was this page helpful?