This article describes the PHP character comparison function similar_text, strnatcmp and strcasecmp usage. Share to everyone for your reference. Specifically as follows:
The ①similar_text () function calculates the number of matching characters for two strings, which can also calculate the similarity of two strings, in percentages.
Syntax: Similar_text (string1,string2,percent)
Note: the Levenshtein () function is faster than the Similar_text () function, but the Similar_text () function provides more precise results with fewer required modifications.
Look at the example below, the code is as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello Peter"; Definition String 2
$result =similar_text ($str 1, $str 2); For comparison
echo $result; Results after the output comparison
The ②strnatcmp () function uses a "natural" algorithm to compare two strings, in the natural algorithm, the number "2" is less than the number "10", and in the computer sort, "2" is greater than "10" because "2" is greater than the first digit of "10", and the function returns:
0-If two strings are equal, <0-if the string1 is less than string2,>0-if string1 is greater than string2.
Syntax: strnatcmp (STRING1,STRING2).
Note: This function is sensitive to case sensitivity.
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =strnatcmp ($str 1, $str 2); Performing comparison operations
echo $result; Output comparison Results
The ③strcasecmp () function compares two strings, which returns:
0-If two strings are equal, <0-if the string1 is less than string2,>0-if string1 is greater than string2.
Syntax: strcasecmp (STRING1,STRING2)
Note: This function is binary safe and insensitive to case, the code is as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =strcasecmp ($str 1, $str 2); Performing comparison operations
echo $result; Output comparison Results
I hope this article will help you with your PHP program design.