PHP substr_compare() function
Example
Compare two strings:
<?php echo substr_compare("Hello world","Hello world",0); ?>
Definition and usage
The substr_compare() function compares two strings from the specified starting position.
Tip:This function is binary safe and optionally case sensitive.
Syntax
substr_compare(string1,string2,startpos,length,case)
Parameters | Description |
---|---|
string1 | Required. Specifies the first string to compare. |
string2 | Required. Specifies the second string to compare. |
startpos | Required. Specifies in string1 . If negative, it counts from the end of the string. |
length | Optional. Specifies where to start the comparison in string1 How many characters to compare in (number of characters). |
case |
Optional. Boolean value, specifying whether to perform a case-sensitive comparison:
|
Technical details
Return value: |
The function returns:
If length Greater than or equal string1 length, the function returns FALSE. |
PHP Version: | 5+ |
Update Log: | Since PHP 5.1, negative numbers are allowed to be used with startpos. |
More Examples
Example 1
Compare two strings when string1 When the starting position for comparison is 6:
<?php echo substr_compare("Hello world","world",6); ?>
Example 2
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); ?>
Example 3
Different Return Values:
<?php echo substr_compare("Hello world!","Hello world!",0); // Two strings are equal echo substr_compare("Hello world!","Hello",0); // string1 Greater than string2 echo substr_compare("Hello world!","Hello world! Hello!",0); // string1 Less than string2 ?>