Hướng dẫn
Quảng cáo

Hàm soundex() trong PHP

Hướng dẫn cách sử dụng hàm soundex() trong lập trình PHP

Tác dụng của hàm soundex()

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

Soundex key is a short alphanumeric string that represent English pronunciation of a word.

The following table summarizes the technical details of this function.

Return Value: Returns the soundex key as a string, or FALSE on failure.
Version: PHP 4+

Syntax

The basic syntax of the soundex() function is given with:

soundex(string);

The following example shows the soundex() function in action.

<?php
// Sample string
$str = "Hello";

// Calculating soundex key
echo soundex($str); // Outputs: H400
?>

Note: The soundex() function returns a 4 characters long string, starting with a letter. Words that pronounced similarly produce the same soundex key, so it can be used to simplify the searches in databases where you know the pronunciation but not the spelling.

Tip: The metaphone() function, which also creates the same key for similar sounding words, is more accurate than soundex(), because it knows the basic rules of English pronunciation.


Parameters

The soundex() function accepts the following parameters.

Parameter Description
string Required. Specifies the input string.

More Examples

Here're some more examples showing how soundex() function actually works:

The following example demonstrates the use of this function on similar sounding words:

<?php
// Sample strings
$str1 = "Desert";
$str2 = "Dessert";

// Calculating soundex key
echo soundex($str1); // Outputs: D263
echo soundex($str2); // Outputs: D263
?>

Bài viết này đã giúp ích cho bạn?

Bài viết mới

Advertisements