PHP Tutorial - PHP metaphone() Function






Definition

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

A metaphone key represents how a string sounds in English. The metaphone() function creates the same key for similar sounding words.

metaphone() is more accurate than the soundex() function.

Syntax

PHP metaphone() Function has the following syntax.

metaphone(string,length)

Parameter

ParameterIs RequiredDescription
stringRequired.String to check
lengthOptional.Maximum length of the metaphone key




Return

PHP metaphone() Function returns the metaphone key of the string on success, or FALSE on failure.

Example 1

Calculate the metaphone key of "World":


<?php
echo metaphone("java2s.com");
?>

The code above generates the following result.

Example 2

Using the metaphone() function on two similar sounding words:


<?php
$str = "phone";
$str2 = "fone";

echo metaphone($str);
echo "\n";
echo metaphone($str2);
?>

The code above generates the following result.





Example 3

Using the length parameter of the metaphone() function on two similar sounding words:


<?php
$str = "phone";
$str2 = "fone";

echo metaphone($str,5);
echo "\n";
echo metaphone($str2,5);
?>

The code above generates the following result.