(PHP 5)
substr_compare — 二進制安全比較字符串(從偏移位置比較指定長度)
說明
int substr_compare ( string$main_str
, string $str
, int $offset
[, int $length
[, bool $case_insensitivity
= false
]] )
substr_compare() 從偏移位置 offset
開始比較 main_str
與 str
,比較長度為 length
個字符。
參數(shù)
main_str
待比較的第一個字符串。
str
待比較的第二個字符串。
offset
比較開始的位置。如果為負數(shù),則從字符串結尾處開始算起。
length
比較的長度。默認值為 str
的長度與 main_str
的長度減去位置偏移量 offset
后二者中的較大者。
case_insensitivity
如果 case_insensitivity
為 TRUE
,比較將不區(qū)分大小寫。
返回值
如果 main_str
從偏移位置 offset
起的子字符串小于 str
,則返回小于 0 的數(shù);如果大于 str
,則返回大于 0 的數(shù);如果二者相等,則返回 0。如果 offset
大于等于 main_str
的長度或 length
被設置為小于 1 的值,substr_compare() 將打印出一條警告信息并且返回 FALSE
。
更新日志
版本
說明
5.1.0
允許使用負數(shù)的 offset
參數(shù)。
范例
Example #1 substr_compare() 范例
<?php
echo substr_compare("abcde", "bc", 1, 2); // 0
echo substr_compare("abcde", "de", -2, 2); // 0
echo substr_compare("abcde", "bcg", 1, 2); // 0
echo substr_compare("abcde", "BC", 1, 2, true); // 0
echo substr_compare("abcde", "bc", 1, 3); // 1
echo substr_compare("abcde", "cd", 1, 2); // -1
echo substr_compare("abcde", "abc", 5, 1); // warning
?>
參見
strncmp() - 二進制安全比較字符串開頭的若干個字符