PHP Function Reference

PHP metaphone() Function



The PHP metaphone() function calculates the metaphone key of a string.

Metaphone key has the property that the words pronounced similarly produce the same metaphone key, and can thus be used to simplify searches in databases where the pronunciation is known but not the spelling.

Note: Similar to this function, soundex() function also creates key based on similar sounding words. But, this function is more accurate than soundex() function as it knows the basic rules of English pronunciation. The function generates keys of variable length while soundex() function generates four character long keys.

Syntax

metaphone(string, max_phonemes)

Parameters

string Required. Specify the input string.
max_phonemes Optional. Specify the maximum length of the returned metaphone key. The default value of 0 means no restriction.

Return Value

Returns the metaphone key as a string.

Example:

The example below shows the usage of metaphone() function.

<?php
echo "metaphone('programming') = "
     .metaphone('programming')."\n";
echo "metaphone('programmer') = "
     .metaphone('programmer')."\n";
?>

The output of the above code will be:

metaphone('programming') = PRKRMNK
metaphone('programmer') = PRKRMR

Example:

Consider one more example where the max_phonemes parameter is used to control the maximum length of the returned metaphone key.

<?php
echo "metaphone('programming', 5) = "
     .metaphone('programming', 5)."\n";
echo "metaphone('programmer', 5) = "
     .metaphone('programmer', 5)."\n";
?>

The output of the above code will be:

metaphone('programming', 5) = PRKRM
metaphone('programmer', 5) = PRKRM

❮ PHP String Reference