Soundex Function

soundex function calculates a sound key for a given string. The soundex key consists of the first letter of the string, followed by three digits that represent the main sound characteristics of the rest of the string. This algorithm is especially useful for finding names that sound similar but may be spelled differently.

Syntax

phpsoundex(string);

Example

Get the soundex key for the string "Hello":

php<?php
  echo soundex('Hello');
?>

Code execution result:

php'H400'

Example

Compare soundex keys for words that sound similar:

php<?php
  $res1 = soundex('Robert');
  $res2 = soundex('Rupert');
  echo $res1 . ' ' . $res2;
?>

Code execution result:

php'R163 R163'

Example

Check soundex keys for different words:

php<?php
  $words = ['Hello', 'Hallo', 'Hullo', 'World'];
  foreach ($words as $word) {
    echo $word . ': ' . soundex($word) . "\n";
  }
?>

Code execution result:

phpHello: H400
Hallo: H400
Hullo: H400
World: W643

See Also

  • levenshtein function,
    which calculates the distance between strings
  • metaphone function,
    which returns the metaphone key for a string