English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP String String Functions Manual
The substr_compare() function is used to compare two strings from the specified starting position.
substr_compare(string1,string2,start position,length,case)
Definition and Usage1 substr_compare() compares string from the offset position startpos2, comparing the length of length characters.
if string1 the substring starting from the offset position startpos is less than string2, then it returns a number less than 0; if greater than string2, then it returns a number greater than 0; if they are equal, then it returns 0. If startpos is greater than or equal to string1 length or length is set to less than 1 value (PHP 5.5.11 In earlier versions), substr_compare() will print a warning message and return FALSE.
Number | Parameters and Description |
---|---|
1 | string1 Required. The first string |
2 | string2 Required. The second string |
3 | startpos Required. It specifies the position to start in string1where to start comparing the string in |
4 | length Optional. Specify the position to start comparison in string1 number of characters involved in the comparison. |
5 | case Boolean value, default is FALSE, case sensitive. If case is TRUE, the comparison will not be case sensitive. |
Try the following example, comparing two strings when string1 the starting position used for comparison is 6 :
<?php //Comparing two strings, when string1 the starting position used for comparison is 6 at echo substr_compare("SAi RAM", "RAM",6); echo '<br>'; //Comparing Strings with Different Parameters echo substr_compare("abcde", "bc", 1, 2); // 0 echo '<br>'; echo substr_compare("abcde", "de", -2, 2); // 0 echo '<br>'; echo substr_compare("abcde", "bcg", 1, 2); // 0 echo '<br>'; echo substr_compare("abcde", "BC", 1, 2, true); // 0 echo '<br>'; echo substr_compare("abcde", "bc", 1, 3); // 1 echo '<br>'; echo substr_compare("abcde", "cd", 1, 2); // -1 echo '<br>'; echo substr_compare("abcde", "abc", 5, 1); // warning ?>Test and see‹/›
Output Result
-5 0 0 0 0 1 -1 PHP Warning: substr_compare(): The start position cannot exceed the initial string...