PHP similar_text() Function

Example

Calculate the similarity between two strings and return the number of matching characters:

<?php
echo similar_text("Hello World","Hello Shanghai");
?>

Run Instance

Definition and Usage

The similar_text() function calculates the similarity between two strings.

This function can also calculate the percentage similarity between two strings.

Note:levenshtein() The function is faster than the similar_text() function. However, the similar_text() function provides more accurate results with fewer required modifications.

Syntax

similar_text(string1,string2,percent)
Parameters Description
string1 Required. Specify the first string to be compared.
string2 Required. Specify the second string to be compared.
percent Optional. Specify the variable name for storing the percentage similarity.

Technical Details

Return Value: Returns the number of matching characters between two strings.
PHP Version: 4+

More Examples

Example 1

Calculate the percentage similarity between two strings:

<?php
similar_text("Hello World","Hello Shanghai",$percent);
echo $percent. "%";
?>

Run Instance