PHP metaphone() Function

What is PHP metaphone() Function?

If you want to generate a phonetic representation of a string in English, use PHP metaphone() function. The function breaks down a string into sounds and creates those “phonetic keys”. So, words that sounds same produce same or similar output they have different spelling.

How metaphone() Function Works?

The metaphone() function converts a word into phonetic keys based on some rules-

  • It converts the string to uppercase. Ex.
    •  “3schools” becomes “3SCHOOLS”.
  • It removes all the non-alphabetic characters (numbers, spaces, punctuation). Ex.
    •  “3SCHOOLS” becomes “SCHOOLS”.
  • It removes silent letters. Ex.
    • K before n. Ex. Knife,
    • W before r. Ex. Wrong
    • Sometimes initial “h”. Ex. Honest.
    • P before s, n, or t. Ex. Psychology, pneumonia
  • Vowels are removed when they affect consonant sounds and first letter is vowel. Examples-
    • “Ant” becomes “ANT”
    • “Belong” becomes “BLNG”
  • It applies phonetic rules-
    • “PH” becomes “F”. Ex. “Photo” to “FOTO”
    • “GH” becomes “F” or ignored. Ex. “Fight” to “FIFT”
    • “TH” becomes “0”. Ex. “Thanks” to “0ANKS”. Note: the “0” digit represents “th” sound.
    • “SH” becomes “X”. Ex. “Shoe” to “XOE”
    • “CH” becomes “X” or “K”. Ex. “Chest” to “XST”
    • “B” becomes “B” (unless at end after ‘M’). Ex. “Birds” to “BIRDS”, “Thumb” becomes “THUM”.
    • “C” becomes “S” (before E, I, Y) or K (otherwise). Ex. “Cat” becomes “KAT”, “Cease” becomes “SEASE”.
    • “D” becomes “J” (before G, E, I, Y) or T (otherwise). Ex. “Data” becomes “TATA”, “Ridge” becomes “RIJE”.
    • “G” becomes “silent” (GN, GNED at end), “J” (before E, I, Y), “K” (otherwise). Ex. “Aligned” becomes “ALINED”, “Gear” becomes “JEAR”, “Goal” becomes “KOAL”.
    • “H” becomes “silent” (after vowel), H (otherwise). Ex. “Honest” becomes “HONEST”.
    • “X” becomes “S (at beginning)”, KS (otherwise). Ex. “X-ray” becomes ”S-RAY”, “Toxin” becomes “TOKSIN”.
  • Remove duplicate sounds. If two letters produce same sounds, one is removed-
    • “Happy” becomes “HAPY”.
  • It uses special symbols to represent sounds-
    • Sound “F” becomes symbol “F”.
    • Sound “Hard C/K” becomes symbol “K”.
    • Sound “SH/CH” becomes symbol “X”.
    • Sound “TH” becomes symbol “0”.
    • Sound “S/C” becomes symbol “S”.
    • Sound “T” becomes symbol “T”.
    • Sound “N” becomes symbol “N”.
    • Sound “M” becomes symbol “M”.
  • Apply length limit. If limit is set by max_phonemes, the function removes extra characters.

Syntax:

metaphone(string, max_phonemes)

Parameters:

The function has 1 required parameter and 1 optional parameter-

string (Required): It specifies a string.

max_phonemes (Optional): It specifies the maximum number of phonetic keys returned by the function. default is 0 which means no limit.

Return Values:

The function returns-

  • Metaphone keys of a string on success or
  • FALSE on failure.

Examples:

Example:

<?php
echo "Metaphone keys of the string \"3Schools of Web.\" is: ";
echo metaphone('3Schools of Web.');
?>

Output:

Metaphone keys of the string "3Schools of Web." is: SXLSFWB

Explanation:

  • The function converts to uppercase. So, the string becomes “3SCHOOLS OF WEB.”.
  • It also removes all non-alphabetic characters. So, it becomes “SCHOOLSOFWEB”.
  • It converts “CH” to “X”. So, it becomes to “SXOOLSOFWEB”.
  • It removes all vowels. So, it becomes to “SXLSFWB”.

Practical Usages of metaphone() Function:

  • It is very useful to find words (ex. in database) that sound same but might be misspelled. Example,
  • It is useful in spelling correction.
  • It improves searching ability.
  • It can compare words based on pronunciation.
  • It can help auto complete / auto suggest similar names.

Difference between metaphone() Function and soundex() Function:

  • metaphone() function is more accurate than soundex() function.
  • Keys returned by the metaphone is variable. On the other hand, it is 4for the soundex() function.
  • metaphone() function handles spelling better than the soundex() function.
  • metaphone() function has more complex algorithm than the soundex() function.
  • metaphone() function is suitable for generalspelling application and finding words that sound similar. On the other hand, soundex() function is suitable for finding simple names.

Notes on metaphone() Function:

  • It was developed by Lawrence Philips.
  • It is more accurate than soundex() function because it uses a English pronunciation rules.
  • It works best with English words.
  • It is case-insensitive as it converts letters to uppercase internally.
  • It removes non alphabetic characters.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP metaphone() Function

metaphone() is a built-in string function in PHP. Use this function to calculate the metaphone keys of a string.

Reference:

https://www.php.net/manual/en/function.metaphone.php