PHP metaphone() Function
The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling applications.
Syntax
1 2 3 |
metaphone ( string $str [, int $phonemes ] ) |
Parameter
str(required)- This parameter specifies the input string.
phonemes(optional)- This parameter specifies the maximum length of the metaphone key and restricts the returned metaphone key. The default value is 0 (means no restriction).
Return
This function returns the metaphone key as a string or FALSE on failure.
Example 1
1 2 3 4 5 6 7 8 9 |
<?php // initializing the string $str= "Hello World"; echo "Actual String: ".$str; // calculating the metaphone key of a string echo "\nMetaphone string: ".metaphone($str); ?> |
Output
1 2 3 4 |
Actual String: Hello World Metaphone string: HLWRLT |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php //initializing the string1 $str1 = "Principle"; echo "String 1: ".$str1; // calculating how a string sounds if said by an English speaking person. echo "\nMetaphone string: ".metaphone($str1); // initializing the string2 $str2 = "Principal"; echo "\n\nString 2: ".$str2; echo "\n"; // calculating how a string sounds if said by an English speaking person. echo "\nMetaphone string: ".metaphone($str2); ?> |
Output
1 2 3 4 5 6 |
String 1: Principle Metaphone string: PRNSPL String 2: Principal Metaphone string: PRNSPL |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?php //initializing the string1 $str1 = "Principle"; echo "String 1: ".$str1; // calculating how a string sounds if said by an English speaking person. echo "\nMetaphone string: ".metaphone($str1,2); // Specifies the length(2) of the metaphone key // initializing the string2 $str2 = "Principal"; echo "\n\nString 2: ".$str2; echo "\n"; // calculating how a string sounds if said by an English speaking person. echo "Metaphone string: ".metaphone($str2,4); // Specifies the length(4) of the metaphone key ?> |
Output
1 2 3 4 5 6 |
String 1: Principle Metaphone string: PR String 2: Principal Metaphone string: PRNS |
Example 4
1 2 3 4 5 6 7 8 9 |
<?php $str = "Year"; $str2 = "ear"; echo metaphone($str,2); echo "\n"; echo metaphone($str2,2); ?> |
Output
1 2 3 |
YR ER |