substr_compare
Binary safe comparison string (compare the specified length from offset position)
substr_compare()
function compares two strings from the specified starting position.
Tip: This function is binary safe and selectively case sensitive.
Compare two strings:
<?php echo substr_compare ( "Hello world" , "Hello world" , 0 ) ; ?>
Try it yourself
Compare two strings, when the start position for comparison in string1 is 6:
<?php echo substr_compare ( "Hello world" , "world" , 6 ) ; ?>
Try it yourself
Use all parameters:
<?php echo substr_compare ( "world" , "or" , 1 , 2 ) ; echo substr_compare ( "world" , "ld" , - 2 , 2 ) ; echo substr_compare ( "world" , "orl" , 1 , 2 ) ; echo substr_compare ( "world" , "OR" , 1 , 2 , TRUE ) ; echo substr_compare ( "world" , "or" , 1 , 3 ) ; echo substr_compare ( "world" , "rl" , 1 , 2 ) ; ?>
Try it yourself
Different return values:
<?php echo substr_compare ( "Hello world!" , "Hello world!" , 0 ) ; // Two strings are equal echo substr_compare ( "Hello world!" , "Hello" , 0 ) ; // string1 is greater than string2 echo substr_compare ( "Hello world!" , "Hello world! Hello!" , 0 ) ; // string1 is less than string2 ?>
Try it yourself
substr_compare ( string1 , string2 , startpos , length , case )
parameter | describe |
---|---|
string1 | Required. Specifies the first string to be compared. |
string2 | Required. Specifies the second string to be compared. |
startpos | Required. Specifies where to start the comparison in string1 . If it is a negative number, count starts from the end of the string. |
length | Optional. Specifies how many characters in string1 are compared (number of characters). |
case |
Optional. Boolean value, specifying whether to perform case-sensitive comparisons:
|