Blog : PHP substr_compare() Function
PHP substr_compare() Function
--------------------------------------------------------------------------------
Complete PHP String Reference
--------------------------------------------------------------------------------
Definition and Usage
The substr_compare() function compares two strings from a specified start position.
This function returns:
0 - if the two strings are equal
<0 - if string1 (from startpos) is less than string2
>0 - if string1 (from startpos) is greater than string2
If length is equal or greater than length of string1, this function returns FALSE.
Syntax
substr_compare(string1,string2,startpos,length,case)
Parameter Description
string1 Required. Specifies the first string to compare
string2 Required. Specifies the second string to compare
startpos Required. Specifies where to start comparing in string1
length Optional. Specifies how much of string1 to compare
case Optional. Specifies whether or not to perform a case-sensitive compare. Default is FALSE (case-sensitive)
--------------------------------------------------------------------------------
Tips and Notes
Tip: This function is binary safe and optionally case-sensitive.
--------------------------------------------------------------------------------
Example 1
echo substr_compare("Hello world","Hello world",0);
?>
The output of the code above will be:
0
--------------------------------------------------------------------------------
Example 2
echo substr_compare("Hello world","world",6);
?>
The output of the code above will be:
0
--------------------------------------------------------------------------------
Example 3
echo substr_compare("Hello world","WORLD",6,TRUE);
?>
The output of the code above will be:
0