PHP String substr_compare()函數
substr_compare()是PHP的內置函數,用于比較兩個字符串的指定起始位置到指定結束位置。愛掏網 - it200.com該函數是一個 二進制安全 的函數,還可以選擇 區分大小寫 。愛掏網 - it200.comPHP 5及以上版本支持此函數。愛掏網 - it200.com
substr_compare()函數的語法如下所示。愛掏網 - it200.com
substr_compare( String main_str, Stringstr, int start_pos, int length, Boolean case-insensitivity = FALSE)
這個函數由五個參數組成,其中三個是必需的,另外兩個是可選的。愛掏網 - it200.com下面是這些參數的描述:
參數
$main_str (必選): 這是這個函數的主字符串參數,需要進行比較。愛掏網 - it200.com這是一個必需參數。愛掏網 - it200.com
$str (必選): 這是這個函數的第二個字符串參數,用來進行比較。愛掏網 - it200.com和$main_str
一樣,這也是一個必需參數。愛掏網 - it200.com
$start_pos (必選): 這是一個必需參數,它具有一個整數值。愛掏網 - it200.com這個參數指定了在$main_str
中從哪個位置開始比較$str。愛掏網 - it200.com換句話說,它提供了比較的起始位置。愛掏網 - it200.com
如果傳遞的值是負數,則從字符串的末尾開始比較。愛掏網 - it200.com
$length (可選): 這個參數在這個函數中不是必須傳遞的。愛掏網 - it200.com它表示比較的長度,也就是指定了要比較$str的多少部分。愛掏網 - it200.com
$case-insensitivity (可選): 這個參數包含一個布爾值,指定是否執行大小寫不敏感的比較。愛掏網 - it200.com它和$length
一樣,是一個可選參數。愛掏網 - it200.com如果$case-insensitivity
是 TRUE 的話,那么比較將是大小寫不敏感的。愛掏網 - it200.com
- FALSE – 大小寫敏感(默認值)
- TRUE – 大小寫不敏感
返回值
這個函數返回以下值:
返回 0 – 如果給定的兩個字符串相等。愛掏網 - it200.com
**返回 < 0 – ** 如果$main_str
(從起始位置)小于$str
。愛掏網 - it200.com
**返回 > 0 – ** 如果$main_str
(從起始位置)大于$str
。愛掏網 - it200.com
注意: 如果$length
參數值等于或大于主字符串($main_str
)的長度,那么這個函數會顯示一個警告并返回FALSE。愛掏網 - it200.com
變更日志
版本 | 描述 |
---|---|
PHP 5.1.0 | 可以使用負的 start_pos。愛掏網 - it200.com |
PHP 5.5.11 | $length 現在可以為0。愛掏網 - it200.com |
PHP 7.2.18, 7.3.5 | $start_pos 可能等于主字符串 ($main_str ) 的長度。愛掏網 - it200.com |
示例
以下給出了一些示例,以了解substr_compare()函數的工作方式。愛掏網 - it200.com
示例1
在下面的示例中,我們在該函數中傳遞了3個必需的參數。愛掏網 - it200.com讓我們看看substr_compare()在三個參數下的工作情況。愛掏網 - it200.com
<?php
main_str = "Good health Good life.";string2 = "Good health Good life.";
// both strings are equal to each other.
echo substr_compare(main_str,string2, 0). "</br>";
// the main string is 11 chracters greater than string2 including whitespace.
echo substr_compare(main_str, "Good health", 0). "</br>";
// the main string is 5 characters shorter than string2 including whitespace.
echo substr_compare(main_str, "Good health Good life. Good", 0). "</br>";
?>
輸出:
0
11
-5
示例2
<?php
// main strting is greater than the second string.
echo substr_compare("Hello javaTpoint","Hello", 0). "</br>";
// both strings are equal, as comparison start at 6th position.
echo substr_compare("Hello javaTpoint","javaTpoint", 6). "</br>";
// main string and next comparable string are not same, so it will return -1
echo substr_compare("Hello javaTpoint","hie", 0). "</br>";
?>
輸出:
11
0
-1
示例3
<?php
// both strings are equal from position 0 to 4.
echo substr_compare("Hello javaTpoint","Hello", 0, 4). "</br>";
// second string is not found between 6 to 10 position, because by default it is case-sensitive.
echo substr_compare("Hello javaTpoint","JAVATPOINT", 6, 10). "</br>";
//As Hello is present in the string, but not found between 5 to 14 range.
echo substr_compare("Hello javaTpoint","Hello", 5, 14). "</br>";
?>
輸出:
0
1
-1
示例4: 大小寫敏感/不敏感
在下面的示例中,我們傳遞了該函數的所有五個參數。愛掏網 - it200.com讓我們通過所有參數來看substr_compare()的工作原理。愛掏網 - it200.com
<?php
main_str = "Good health Good life.";string2 = "Good health Good life.";
case_insensitivity = FALSE;
// here, this function works as case-insensitive
echo substr_compare(main_str, string2, 0, 9, TRUE). "</br>";
// here, this function case-sensitive echo substr_compare(main_str, "GOOD Health", 0, 11, case_insensitivity). "</br>";
//the function works as case-insensitive
echo substr_compare(main_str, "GOOD health", 0, 11, TRUE). "</br>";
?>
輸出:
0
1
0