PHP similar_text() function
Instance
Calculate the similarity between two strings and return the number of matching characters:
<?php echo similar_text("Hello World","Hello Shanghai"); ?>
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 through fewer required modifications.
Syntax
similar_text(string1,string2,percent)
Parameter | 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. "%"; ?>