Instance
Comparison of two strings:
<?phpecho substr_compare ("Hello World", "Hello World", 0);? >
Definition and usage
The Substr_compare () function compares two strings from a specified starting position.
Tip: This function is binary safe and selective case-sensitive.
Grammar
Substr_compare (String1,string2,startpos,length,case)
| Parameters |
Describe |
| String1 |
Necessary. Specifies the first string to compare. |
| string2 |
Necessary. Specifies a second string to compare. |
| Startpos |
Necessary. Specify where in the string1 to begin the comparison. If negative, the count starts at the end of the string. |
| Length |
Optional. Specifies the number of characters to participate in the comparison in string1. |
| Case |
Optional. A Boolean value that specifies whether a case-sensitive comparison is performed:
|
technical details
| return value: |
This function returns:
-
0-if two strings are equal
-
<0-if string1 (from start position startpos) is less than string2
-
>0-if string1 (startpos from start) is greater than string2
if length is greater than or equal to the length of string1, the function returns FALSE. |
| php version: |
5+ |
| update log: |
from PHP 5.1, negative startpos are allowed. |
More examples
Example 1
Compares two strings when the starting position for comparison in string1 is 6 o'clock:
<?phpecho substr_compare ("Hello World", "World", 6);? >
Example 2
Use all the parameters:
<?phpecho Substr_compare ("World", "or",;); echo Substr_compare ("World", "LD", -2,2); echo Substr_compare ("World", "ORL",); echo Substr_compare ("World", "OR", 1,2,true); echo Substr_compare ("World", "or", 1, 3); echo Substr_compare ("World", "RL"); >
Example 3
Different return values:
<?phpecho substr_compare ("Hello world!", "Hello world!", 0); The strings is 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?>