substr_compare – PHP String Functions
Syntax :
Description :
It’s an inbuilt function of PHP. substr_compare() function will compares two strings from a specified start position. It do the binary safe comparison of two strings.
Parameter :
[table caption=”” width=”100%” colwidth=”15%|15%|15%|55%” colalign=”left|left|left|left”]
Name, Required /Optional, Value Type, Description
string1, Required, String, First string to compare.
string2, Required, String, Second string to be compared.
start_position, Required, String, Specifies where to start comparing in string1.. If value is negative then it starts counting from the end of the string.
length, Required, String, Specifies how much of string1 to compare.
case_insensitivity, Optional, Boolean , A boolean value that specifies whether or not to perform a case-sensitive compare. FALSE – Default. Case-sensitive. TRUE – Case-insensitive.
[/table]
Output :
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.
ChangeLog :
[table caption=”” width=”100%” colwidth=”50%|50%” colalign=”left|left”]
Version, Description
PHP 5.5.11 , length may now be 0..
PHP 5.1.0, Added the possibility to use a negative offset.
[/table]
Related articles : strncmp().
substr_compare() – PHP Functions Example 1 : if both string1 and string2 are same it returns 0.
<?php echo substr_compare("Tutorialmines.net","Tutorialmines.net",0); ?>
In above example, We have same strings to be compared. Output of above code in the browser is as below:
substr_compare() – PHP Functions Example 2 : Compare two strings, when start position in string1 for the comparison is 6th:
<?php echo substr_compare("Hello readers","readers",6); ?>
In above example ,We have a string1 “Hello readers” and string2 “readers”. This will compare string1 from 6th position. Output of above code in the browser is as below:
substr_compare() – PHP Functions Example 3 : Using all possible parameters
<?php // PHP program to show // substr_compare() function echo substr_compare("tutor","or",3,2)."\n"; //start compare from the last of the string echo substr_compare("tutor","or",-2,2)."\n"; //start compare from the last of the string echo substr_compare("tutor","uto",1,2)."\n"; //Case-insensitive compare echo substr_compare("tutor","UT",1,2,TRUE)."\n"; //Case-sensitive compare echo substr_compare("tutor","UT",1,2)."\n"; echo substr_compare("tutor","ut",1,4)."\n"; echo substr_compare("tutor","ut",1,2)."\n"; ?>
Output of above code is :
0
0
0
0
32
2
0
substr_compare() – PHP Functions Example 4 : Different return values:
<?php echo "\n".substr_compare("Hello readers!","Hello readers!",0); // the two strings are equal echo "\n".substr_compare("Hello readers!","Hello",0); // string1 is greater than string2 echo "\n".substr_compare("Hello readers!","Hello readers here! ",0); // string1 is less than string2 ?>
Output of above code is :
0
9
33