W3docs

soundex()

Our article is about the PHP function soundex(), which is used to calculate the soundex key of a string. This function is useful for comparing the pronunciation

The PHP soundex() function calculates the Soundex key of a string — a short code that represents how the string sounds in English rather than how it is spelled. Two words that are spelled differently but pronounced similarly (such as "Smith" and "Smyth") produce the same key, which makes Soundex handy for fuzzy name matching, spell-check suggestions, and de-duplicating contact lists.

This page covers the syntax of soundex(), the format of the key it returns, how the algorithm works, and practical examples — including its limitations and how it compares to related functions.

Syntax

soundex(string $string): string

It takes one parameter:

  • $string — the input string to encode. Only the alphabetic characters are considered; digits, spaces, and punctuation are ignored.

It returns the Soundex key as a string. For non-empty alphabetic input the key is always 4 characters: one uppercase letter followed by three digits (for example, H464). If the input contains no letters, soundex() returns an empty string.

How the Soundex key is built

The Soundex algorithm reduces a word to its first letter plus a three-digit code based on the remaining consonant sounds:

  1. Keep the first letter of the word.

  2. Map the remaining letters to digits, grouping consonants that sound alike:

    LettersDigit
    b, f, p, v1
    c, g, j, k, q, s, x, z2
    d, t3
    l4
    m, n5
    r6
  3. The vowels a, e, i, o, u and the letters h, w, y are dropped (they are not assigned a digit).

  4. Adjacent duplicate digits are collapsed into one.

  5. The result is padded with zeros (or truncated) to exactly four characters.

This is why soundex('Robert') and soundex('Rupert') both produce R163 — the differing vowels are ignored.

Basic example

php— editable, runs on the server

Here the string $string is encoded with soundex(). The output is:

H464

The H is the first letter, and 464 encodes the consonant sounds l (4), r (6), l (4). The space and the word boundary are ignored — Soundex treats the input as one run of letters.

Comparing two strings

The most common use of soundex() is checking whether two words sound the same by comparing their keys:

php— editable, runs on the server

Since Smith and Smyth both encode to S530, their keys match and the output is:

The strings sound the same.

Matching a name against a list

A practical pattern is suggesting "did you mean…" candidates from a known list when an exact match fails:

<?php
$names = ['Robert', 'Rupert', 'Rubin', 'Albert'];
$query = 'Ruppert';
$queryKey = soundex($query);

foreach ($names as $name) {
    if (soundex($name) === $queryKey) {
        echo "$name sounds like $query\n";
    }
}
?>

Both Robert and Rupert share the key R163 with Ruppert, so the output is:

Robert sounds like Ruppert
Rupert sounds like Ruppert

Limitations

  • English-centric. The letter-to-digit mapping is tuned for English pronunciation, so it works poorly for names from other languages.
  • First letter must match. Because Soundex always keeps the first letter, Kris (K620) and Chris (C620) do not match, even though they sound identical.
  • Coarse matching. Only the first few consonant sounds survive, so very different long words can collide on the same key. Use it as a first-pass filter, not a final answer.

For a different phonetic algorithm that often handles English better, see metaphone(). To measure how close two strings are rather than whether they sound alike, see similar-text() and levenshtein().

Practice

Practice
What is the function of the Soundex system in PHP?
What is the function of the Soundex system in PHP?
Was this page helpful?