Friday, October 10, 2025
HomeLanguagesPHP metaphone() Function

PHP metaphone() Function

The metaphone() function is a built-in function in PHP and is used to calculate the metaphone key of a given string. The Metaphone key is a phonetic algorithm for indexing of words by their pronunciation. It uses the larger set of rules for English pronunciation. 

Syntax:

string metaphone ( $str, $key )

Parameters: This function accepts two parameters as mentioned above and described below:

  • $str: It is Required parameter which represents the string to find the Metaphone key.
  • $key: It is an optional parameter which specified for Maximum length of the metaphone key.

Return Value: It returns the metaphone key as a string, and return FALSE in failure. Examples:

Input: $str = "Contribute Article on neveropen"
Output: KNTRBTRTKLNJKSFRJKS

Input: $str = "Contribute Article on neveropen"  $key = 5
Output: KNTRB

Below programs illustrate the metaphone() function in PHP.

Program 1: 

php




<?php
$str1 = "Contribute Article on neveropen";
echo metaphone($str1) . "\n";
  
$str2 = "A computer science portal";
echo metaphone($str2);
?>


Output:

KNTRBTRTKLNJKSFRJKS
AKMPTRSNSPRTL

Program 2: 

php




<?php
$str1 = "Contribute Article on neveropen";
echo metaphone($str1, 6) . "\n";
  
$str2 = "A computer science portal";
echo metaphone($str2, 5);
?>


Output:

KNTRBT
AKMPT

Related Article: PHP | soundex() Function 

Reference: http://php.net/manual/en/function.metaphone.php

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS