The similar_text () function calculates the number of matching characters for two strings.
This function can also calculate the similarity between two strings (expressed in percentages ).
Syntax
Similar_text (string1, string2, percent)
Note: The levenshtein () function is faster than the similar_text () function. However, the similar_text () function provides more accurate results with fewer necessary modifications.
See the instance below
*/
$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello peter"; // defines string 2
$ Result = similar_text ($ str1, $ str2); // compare
Echo $ result; // output the comparison result.
/*
The strnatcmp () function uses a "natural" algorithm to compare two strings.
In natural algorithms, the number "2" is less than the number "10 ". In computer sorting, "2" is greater than "10" because "2" is greater than "10.
This function returns:
0-if the two strings are equal
<0-If string1 is smaller than string2
> 0-If string1 is greater than string2
Syntax
Strnatcmp (string1, string2)
Note: This function is case sensitive.
*/
$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello world"; // defines string 2
$ Result = strnatcmp ($ str1, $ str2); // perform the comparison operation.
Echo $ result; // output the comparison result.
/*
The strcasecmp () function compares two strings.
This function returns:
0-if the two strings are equal
<0-If string1 is smaller than string2
> 0-If string1 is greater than string2
Syntax
Strcasecmp (string1, string2)
Note: This function is binary secure and case-insensitive.
*/
$ Str1 = "hello world"; // defines string 1
$ Str2 = "hello world"; // defines string 2
$ Result = strcasecmp ($ str1, $ str2); // perform the comparison operation.
Echo $ result; // output the comparison result.