This example summarizes the PHP common string comparison functions. Share to everyone for your reference. The specific analysis is as follows:
The substr_compare () function compares two strings from the specified start length , which returns:
0-If the two strings are equal, <0-if the string1 (from the start position) is less than string2,>0-if the string1 (from the start position) is greater than the string2.
Syntax: Substr_compare (string1,string2,startpos,length,case), code as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =substr_compare ($str 1, $str 2,1,10); Performing comparison operations
echo $result; Output results, 1
the strnatcasecmp () function uses a "natural" algorithm to compare two strings, in the natural algorithm, the number "2" is less than the number "10", in the computer sort, "2" is greater than "10", because "2" is greater than the first digit of "10", 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 =strnatcasecmp ($str 1, $str 2); Performing comparison operations
echo $result; Output results, 0
The strncasecmp () 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: strncasecmp (string1,string2,length), code as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =strncasemp ($str 1, $str 2,7); Performing comparison operations
echo $result; Output results, 0
strncmp () function compares two strings, 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: strncmp (string1,string2,length), code as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =strncmp ($str 1, $str 2,7); Performing comparison operations
echo $result; Output results, 1
strcoll () function compares two strings, the function returns:
0-If two strings are equal, <0-if the string1 is less than string2,>0-if string1 is greater than string2.
The comparison of strings varies according to local settings, A<a or A>a.
Syntax: Strcoll (STRING1,STRING2), code as follows:
Copy Code code as follows:
$str 1= "Hello World"; Definition string 1
$str 2= "Hello World"; Definition String 2
$result =strcoll ($str 1, $str 2); Performing comparison operations
echo $result; Output results, 1
I hope this article will help you with your PHP program design.