How to Use metaphone() Function in PHP 8.1 and 8.2 with Example?

Hello Friends Today, through this tutorial, I will tell you How to Use `metaphone()` function using PHP, PHP 8, PHP 8.1, PHP 8.2 With Example. In PHP, the `metaphone()` function is used for phonetic comparison of strings. It generates a key based on the pronunciation of a word which can be used for comparing words that sound similar. Here’s how you can use the `metaphone()` function in PHP 8.1 and 8.2 with an example:

<?php
// Example words
$words = array("write", "right", "rain", "rein");

// Calculate metaphone keys for each word
foreach ($words as $word) {
echo "Metaphone for '$word': " . metaphone($word) . "\n";
}

// Compare metaphone keys of two words
$word1 = "write";
$word2 = "right";

echo "\nComparing '$word1' and '$word2':\n";

if (metaphone($word1) === metaphone($word2)) {
echo "These words sound similar!\n";
} else {
echo "These words sound different.\n";
}
?>

Output:

Metaphone for 'write': RT
Metaphone for 'right': RT
Metaphone for 'rain': RN
Metaphone for 'rein': RN

Comparing 'write' and 'right':
These words sound similar!

In this example:

1. We have an array of words for which we want to calculate the metaphone keys.
2. We loop through each word and calculate its metaphone key using the `metaphone()` function.
3 We then compare the metaphone keys of two words (`write` and `right`) to see if they sound similar.