PHP String substr_count() 函數(shù)
substr_count() 是 PHP 的內(nèi)置函數(shù)。愛掏網(wǎng) - it200.com它統(tǒng)計給定字符串中子字符串出現(xiàn)的次數(shù)。愛掏網(wǎng) - it200.comsubstr_count() 是區(qū)分大小寫的函數(shù),這意味著它對大寫字母和小寫字母進行不同處理。愛掏網(wǎng) - it200.com
例如 – 子字符串 “cha” 在字符串 “CHaru” 中不存在。愛掏網(wǎng) - it200.com
注意: substr_count() 是區(qū)分大小寫的函數(shù)。愛掏網(wǎng) - it200.com
該函數(shù)提供了一種計算子字符串在主字符串中出現(xiàn)的次數(shù)的方法。愛掏網(wǎng) - it200.com它還提供了在給定索引范圍內(nèi)搜索子字符串的選項。愛掏網(wǎng) - it200.com如果為搜索指定的起始位置和長度數(shù)值大于傳入的字符串,則向用戶返回一個警告。愛掏網(wǎng) - it200.com PHP 4 及以上版本 支持此函數(shù)。愛掏網(wǎng) - it200.com
注意: substr_count() 函數(shù)不計算重疊的子字符串。愛掏網(wǎng) - it200.com(見示例3)。愛掏網(wǎng) - it200.com
substr_count() 的語法如下,它接受四個參數(shù),兩個字符串和兩個整數(shù)類型的值。愛掏網(wǎng) - it200.com
substr_count(string,substring, start,length)
參數(shù)
$string(必需): $string
參數(shù)是要計算子字符串出現(xiàn)次數(shù)的主要字符串參數(shù)。愛掏網(wǎng) - it200.com它是substr_count()函數(shù)的必需參數(shù)。愛掏網(wǎng) - it200.com
$substring(必需): 在此參數(shù)中傳遞的值將在$string
參數(shù)中搜索,并返回子字符串的計數(shù)出現(xiàn)次數(shù)。愛掏網(wǎng) - it200.com這也是substr_count()函數(shù)的必需參數(shù)。愛掏網(wǎng) - it200.com
$start(可選): 該參數(shù)由一個整數(shù)值構成。愛掏網(wǎng) - it200.com它指定從何處開始計數(shù)。愛掏網(wǎng) - it200.com$start
是此函數(shù)的可選參數(shù)。愛掏網(wǎng) - it200.com如果在此參數(shù)中傳遞的值為負數(shù),則會從字符串的末尾開始計數(shù)。愛掏網(wǎng) - it200.com
$length(可選): 該參數(shù)是可選的,并且取決于$start
參數(shù)。愛掏網(wǎng) - it200.com如果$start
和$length
的組合(start +length)
大于$string
的長度,則substr_count()會生成警告。愛掏網(wǎng) - it200.com負數(shù)長度總是從字符串的末尾計數(shù)。愛掏網(wǎng) - it200.com
返回值
substr_count返回一個整數(shù),表示子字符串在主字符串中出現(xiàn)的次數(shù)。愛掏網(wǎng) - it200.com
變更日志
- 在PHP 5.1.0中添加了兩個新參數(shù),即
$start
和$length
。愛掏網(wǎng) - it200.com - 從PHP 7.1.0開始,
$star
和$length
也支持負值。愛掏網(wǎng) - it200.com現(xiàn)在$length
也可以是0。愛掏網(wǎng) - it200.com
示例
以下給出了一些示例。愛掏網(wǎng) - it200.com通過這些示例,學習在程序中實際實現(xiàn)substr_count()函數(shù)的方法。愛掏網(wǎng) - it200.com
示例1: 不帶有可選參數(shù)的程序。愛掏網(wǎng) - it200.com
<?php
strin = "Good Health Good Life";sub_str = "Good";
echo substr_count(strin,sub_str);
?>
輸出:
在上面的示例中,”Good”子字符串在主字符串中出現(xiàn)了2次。愛掏網(wǎng) - it200.com
2
示例2: 當傳入$start
參數(shù)時的程序。愛掏網(wǎng) - it200.com
<?php
strin = "Good Health Good Life";sub_strin = "Good";
echo substr_count(strin,sub_strin, 5);
?>
輸出:
在上面的示例中,”Good”子字符串在主字符串中只出現(xiàn)一次,因為搜索是從第5個位置開始的。愛掏網(wǎng) - it200.com
1
示例3: 程序當start和length兩個參數(shù)都傳遞時。愛掏網(wǎng) - it200.com
<?php
strin = "Good Health Good Life";sub_strin = "Good";
echo substr_count(strin,sub_strin, 5, 10);
?>
輸出:
在上面的示例中,“Good”子字符串在主字符串中沒有被找到,因為搜索是從第5個位置開始到第15個位置(start+length)
。愛掏網(wǎng) - it200.com
0
示例 4: 區(qū)分大小寫
<?php
strin = "Good Health Good Life";sub_strin = "LIFE";
echo substr_count(strin,sub_strin);
?>
輸出:
在上面的示例中,由于substr_compare()是區(qū)分大小寫的函數(shù),所以在主字符串中找不到”LIFE”子字符串。愛掏網(wǎng) - it200.com
0
示例5:
<?php
strin = "Hello javaworld";sub_strin = "Hello";
echo substr_count(strin,sub_strin, 5,12);
?>
輸出:
這里發(fā)生了一個警告,因為start + length > strin,即(5+12 > 15)。愛掏網(wǎng) - it200.com$start + $length
參數(shù)的長度不應超過主字符串的長度。愛掏網(wǎng) - it200.com
Warning: substr_count(): Invalid length value in C:\xampp\htdocs\xampp\PMA\substr_count.php on line 4
示例6: 重疊的子字符串
<?php
strin = "Hello javava";sub_strin = "java";
echo substr_count(strin,sub_strin);
?>
輸出:
它返回了1,因為它沒有計算重疊的子字符串。愛掏網(wǎng) - it200.com
1