PHP 字符串 strspn()函數(shù)
strspn()函數(shù)是PHP的一個(gè)內(nèi)建函數(shù)。愛(ài)掏網(wǎng) - it200.com此函數(shù)用于查找放置在另一個(gè)字符串中的字符串的初始段的長(zhǎng)度。愛(ài)掏網(wǎng) - it200.com簡(jiǎn)單來(lái)說(shuō),strspn()函數(shù)幫助我們找到“在另一個(gè)字符串中有多少個(gè)字符?”它返回僅包含$charsearch參數(shù)中字符的字符串中找到的字符總數(shù)。愛(ài)掏網(wǎng) - it200.com
它是 區(qū)分大小寫(xiě) 的函數(shù),區(qū)分大寫(xiě)和小寫(xiě)字符。愛(ài)掏網(wǎng) - it200.com
strspn()的語(yǔ)法如下,由四個(gè)參數(shù)組成。愛(ài)掏網(wǎng) - it200.com
strspn( string,charsearch, start_from,length)
參數(shù)
strspn()函數(shù)有四個(gè)參數(shù),如上所示的語(yǔ)法。愛(ài)掏網(wǎng) - it200.com其中,有兩個(gè)參數(shù)是必需的,而另外兩個(gè)參數(shù)是可選的。愛(ài)掏網(wǎng) - it200.com下面是所有這些參數(shù)的詳細(xì)信息:
$string (必需): 這是一個(gè)必需的字符串參數(shù),其中我們查找搜索字符串。愛(ài)掏網(wǎng) - it200.com該參數(shù)指定要搜索的字符串。愛(ài)掏網(wǎng) - it200.com
$charsearch (必需): 這也是一個(gè)必需的參數(shù),用于指定要在給定的$string參數(shù)中搜索的字符列表。愛(ài)掏網(wǎng) - it200.com
$start (可選): 這個(gè)參數(shù)是可選的,它指定我們從$string參數(shù)的哪里開(kāi)始搜索。愛(ài)掏網(wǎng) - it200.com該參數(shù)保存一個(gè)整數(shù)值。愛(ài)掏網(wǎng) - it200.com
- 如果 $start 存在且為非負(fù)值,則從$start參數(shù)中給定的位置開(kāi)始搜索。愛(ài)掏網(wǎng) - it200.com
- 如果 $start 存在且包含負(fù)值,則strspn()函數(shù)將從string中該位置的末尾開(kāi)始搜索charsearch。愛(ài)掏網(wǎng) - it200.com
$length (可選): 這是一個(gè)可選參數(shù),用于定義要在string中搜索的字符數(shù)。愛(ài)掏網(wǎng) - it200.com如果未提供length參數(shù),則默認(rèn)情況下它將檢查整個(gè)$string。愛(ài)掏網(wǎng) - it200.com
- 如果 $length 參數(shù)存在且具有正值,則從開(kāi)始到length值,在string中搜索$charsearch。愛(ài)掏網(wǎng) - it200.com
- 如果 $length 參數(shù)存在且具有負(fù)值,則從開(kāi)始位置到string的末尾的第length個(gè)字符,搜索$charsearch。愛(ài)掏網(wǎng) - it200.com
返回值
strspn()函數(shù)返回最初在string中找到的字符總數(shù),該字符由charsearch參數(shù)包含。愛(ài)掏網(wǎng) - it200.com
如果在string中不存在charsearch的任何字符,則返回0。愛(ài)掏網(wǎng) - it200.com
注意: 長(zhǎng)度和起始參數(shù)支持PHP 4.3及以上版本。愛(ài)掏網(wǎng) - it200.com
示例
以下是一些示例,這些示例將幫助你更輕松和快速地理解strspn()函數(shù)的工作方式。愛(ài)掏網(wǎng) - it200.com
Input
string = ABCDEFGHIJKL,charsearch = ABCDGHI
Output = 4
Input
string = ABCDEFGHIJKL,charsearch = abc //case-sensitive
Output = 0
Input
string = ABCDEFGHIJKL,charsearch = ABCDE, start = 2
Output = 3
示例1
這是strspn()函數(shù)的基礎(chǔ)和非常簡(jiǎn)單的示例。愛(ài)掏網(wǎng) - it200.com
<?php
echo strspn( "Hie! Welcome to javaTpoint", "Hie!" );
?>
輸出:
4
示例2
<?php
main_str1 = "Good Morning!";search_str1 = "MornGbs!";
match_char1 =strspn(main_str1, search_str1);
echo "Number of characters matched = " .match_char1;
?>
輸出:
在這個(gè)示例中,我們沒(méi)有提供開(kāi)始和長(zhǎng)度參數(shù),所以它從字符串的開(kāi)頭開(kāi)始搜索到結(jié)束。愛(ài)掏網(wǎng) - it200.com發(fā)現(xiàn)有3個(gè)字符與主字符串匹配。愛(ài)掏網(wǎng) - it200.com
Number of characters matched = 3
示例3
<?php
main_str1 = "Good Morning!";search_str1 = "Morning! Good";
match_char1 =strspn(main_str1, search_str1);
echo "Number of characters matched = " .match_char1;
?>
輸出:
在這個(gè)示例中,整個(gè) $search_str1 字符串在主字符串中找到了,所以返回了13個(gè)匹配的字符。愛(ài)掏網(wǎng) - it200.com這個(gè)函數(shù)將”Good Morning!”和”Morning! Good”視為相似的。愛(ài)掏網(wǎng) - it200.com
Number of characters matched = 13
示例4: 區(qū)分大小寫(xiě)
<?php
main_str1 = "Good Morning!";search_str1 = "GOOD MORNING!";
match_char1 =strspn(main_str1, search_str1);
echo "Number of characters matched = " .match_char1;
?>
輸出:
該函數(shù)返回1,因?yàn)槌薌之外,它在處理”Good Morning!”和”GOOD MORNING!”時(shí)是有區(qū)分大小寫(xiě)的。愛(ài)掏網(wǎng) - it200.com只有兩個(gè)字符串的首字母相互匹配。愛(ài)掏網(wǎng) - it200.com這證明了以下函數(shù)是區(qū)分大小寫(xiě)的。愛(ài)掏網(wǎng) - it200.com
Number of character matches = 1
示例5
<?php
main_str1 = "abcdefghij";search_str1 = "AB";
match_char1 =strspn(main_str1, search_str1);
echo "Number of characters matched = " .match_char1;
?>
輸出:
在這個(gè)示例中,strspn() 函數(shù)返回了0,因?yàn)?“AB” 與主字符串不匹配。愛(ài)掏網(wǎng) - it200.com
Number of characters matched = 0