metaphone()
metaphone() — Calculate the metaphone key of a string.
Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.
What it does
Similar to soundex() metaphone creates the same key for similar sounding words. It's more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length.
Signature
metaphone(string $string, int $max_phonemes = 0): string
Parameters
| Parameter | Type | What it takes |
|---|---|---|
| $string | string | The input string. |
| $max_phonemes | int | This parameter restricts the returned metaphone key to max_phonemes characters in length. However, the resulting phonemes are always transcribed completely, so the resulting string length may be slightly longer than max_phonemes. The default value of 0 means no restriction. |
Return value
Returns the metaphone key as a string.
Example
A runnable snippet:
var_dump(metaphone('programming')); var_dump(metaphone('programmer'));
Output:
string(7) "PRKRMNK" string(6) "PRKRMR"
Changelog
| Version | Change |
|---|---|
| 8.0.0 | The function returned false on failure. |
See also
levenshtein(), similar_text(), soundex()
All PHP 8.5 built-in functions
Reference card built from the PHP manual page metaphone() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.