Compare two strings:
<?phpecho substr_compare("Hello world","Hello world",0);?>The substr_compare() function compares two strings starting at a specified starting position.
Tip: This function is binary safe and selectively case-sensitive.
substr_compare( string1,string2,startpos,length,case )
parameter | describe |
---|---|
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to be compared. |
startpos | Required. Specifies where in string1 to start comparison. If negative, counting starts from the end of the string. |
length | Optional. Specifies the number of characters in string1 to be compared. |
case | Optional. A Boolean value that specifies whether to perform case-sensitive comparisons: FALSE - Default. case sensitive TRUE - not case sensitive |
Return value: | The function returns: 0 - if the two strings are equal <0 - if string1 (startposition startpos) is less than string2 >0 - if string1 (startposition startpos) is greater than string2 If length is greater than or equal to the length of string1, this function returns FALSE. |
---|---|
PHP version: | 5+ |
Update log: | As of PHP 5.1, negative startpos is allowed. |
Compare two strings when the starting position for comparison in string1 is 6:
<?phpecho substr_compare("Hello world","world",6);?>Use all parameters:
<?phpecho 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);?>Different return values:
<?phpecho substr_compare("Hello world!","Hello world!",0); // the two strings are equalecho substr_compare("Hello world!","Hello",0); // string1 is greater than string2echo substr_compare ("Hello world!","Hello world! Hello!",0); // str1 is less than str2?>