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 Example

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 necessary modifications.

Syntax

similar_text(string1,string2,percent)
Parameter Description
string1 Required. Specifies the first string to be compared.
string2 Required. Specifies the second string to be compared.
percent Optional. Specifies 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 Example