The metaphone Function

The metaphone function returns the metaphone of a string - a phonetic key that allows comparing words by their sound. The first parameter is the string to process, and the second (optional) one is the maximum length of the returned metaphone.

Syntax

phpmetaphone(string, [phonemes]);

Example

Let's calculate the metaphone for the word "programming":

php<?php
  echo metaphone('programming');
?>

Code execution result:

php'PRKRMNK'

Example

Let's compare metaphones for words that sound similar:

php<?php
  $res1 = metaphone('write');
  $res2 = metaphone('right');
  var_dump($res1 === $res2);
?>

Code execution result:

phptrue

Example

Let's limit the metaphone length to 4 characters:

php<?php
  echo metaphone('international', 4);
?>

Code execution result:

php'ANTR'

See Also

  • the soundex function,
    which calculates the soundex-key of a string
  • the levenshtein function,
    which calculates the distance between strings