soundex

Calculates a four-character phonetic key so similar-sounding words share the same code.

Signature
soundex(string $string): string
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$stringstringThe input string.

Return value

stringA four-character soundex key. Starts with a letter if the input contains at least one letter, otherwise "0000".

Changelog

  • PHP 8.0.0Prior to this version, calling the function with an empty string returned false. It now returns "0000".

soundex reduces a word to a four-character key — one letter followed by three digits — so that words pronounced alike collapse to the same value. Robert and Rupert both give R163; Smith, Smyth and Schmidt all give S530. The classic use is searching a name column when the caller spelled the name by ear.

How to use it in practice

Store the key alongside the value in an indexed column, then look up by key rather than computing soundex over every row at query time. MySQL and PostgreSQL both have their own SOUNDEX, but the implementations differ in edge cases, so compute the key in one place and stick to it.

Know the limits

The algorithm is the one Knuth describes, and it was designed for English surnames. It always keeps the first letter, so Carr and Karr never match despite sounding identical, and it is useless for non-Latin scripts or for languages whose phonetics differ from English. Because it only produces about 26 × 1000 possible keys, false positives are common in a large table — treat a soundex hit as "candidate", then rank the candidates with levenshtein or similar_text.

metaphone is generally more accurate for English words and produces variable-length keys; try it when soundex's collisions are too coarse.

Example

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

When to use it

  • Offer "sounds like" results when a customer-service search for a surname returns nothing.
  • Group likely spelling variants of a supplier name during a data cleanup.
  • Pre-filter a large name table down to candidates before running a costlier similarity comparison.

Frequently asked questions

What is the difference between soundex and metaphone?

soundex always returns a four-character key and keeps the first letter verbatim; metaphone returns a variable-length key and applies real English pronunciation rules, including silent letters. metaphone is usually more accurate, soundex is simpler and matches most database SOUNDEX implementations.

Does soundex work with non-English names or accented characters?

Not reliably. The algorithm was built for English surnames and processes bytes, so accented and non-Latin characters are effectively ignored. Transliterate to ASCII first, and expect poor results for other languages' phonetics.

What does soundex return for a string with no letters?

"0000". Since PHP 8.0 an empty string also returns "0000"; earlier versions returned false for empty input.

More examples

A sounds-like name search

Smith, Smyth and Schmidt all reduce to S530 and match the search term Smithe, while Jones (J520) does not. Schmidt matching is both the strength and the weakness on display: it is a genuine spelling variant, but the same coarseness will pull in unrelated names on a table of any size.

Example · php
<?php
$names = ['Smith', 'Smyth', 'Schmidt', 'Jones'];
$search = 'Smithe';
$key = soundex($search);

foreach ($names as $name) {
    printf("%-8s %s %s\n", $name, soundex($name), soundex($name) === $key ? 'MATCH' : '');
}

Soundex versus metaphone

Soundex calls Knuth and Kant identical (both K530) even though they sound quite different, and it separates Wright (W623) from Rita (R300) because it blindly keeps the leading W. Metaphone handles the silent W, giving RFT and RT. The Bear/Beer row is the case both get right.

Example · php
<?php
$pairs = [['Knuth', 'Kant'], ['Bear', 'Beer'], ['Wright', 'Rita']];

foreach ($pairs as [$a, $b]) {
    printf("%-8s %-8s soundex %s/%s  metaphone %s/%s\n",
        $a, $b, soundex($a), soundex($b), metaphone($a), metaphone($b));
}

Discussion

  • Be the first to comment on this lesson.