Skip to content

soundex()

The soundex() function calculates a soundex key for a string, which is useful for comparing the pronunciation of two strings. Below is the syntax and usage.

The PHP syntax of the soundex()

php
string soundex ( string $str )

The function takes one parameter: $str, which is the string to be encoded. It always returns a 4-character string, padding with zeros if necessary.

Example of PHP soundex()

php
<?php
$string = 'Hello World';
$soundex = soundex($string);
echo $soundex;
?>

In this example, we have a string variable $string that we want to encode using the soundex() function. The output of this code will be:

console
H464

As you can see, the soundex() function has calculated the soundex key of the string.

Comparing two strings

php
<?php
$str1 = 'Smith';
$str2 = 'Smyth';
if (soundex($str1) === soundex($str2)) {
    echo "The strings sound the same.";
} else {
    echo "The strings sound different.";
}
?>

Since Smith and Smyth have the same pronunciation, their soundex keys match, and the output will be:

console
The strings sound the same.

The soundex() function is a useful tool for comparing the pronunciation of two strings. It allows you to calculate the soundex key of a string, which is a phonetic algorithm that generates a code based on the way a word sounds.

Practice

What is the function of the Soundex system in PHP?

Do you find this helpful?

Dual-run preview — compare with live Symfony routes.