【函数功能】
计算字符串中全部字符都存在于指定字符集合中的第一段子串的长度(计算一个字符串中连续匹配另一个字符串的长度)。
【函数语法】
strspn ($subject, $mask, $start = null, $length = null)
【参数说明】
$subject:待检查的字符串。
$mask:检查字符列表。
$start:$subject 的开始检查位置。
如果 $start 被设置并且是非负的,strspn() 将从 $subject 的第 $start 个位置开始检查。
如果 $start 被设置并且为负数,strspn() 将从 $subject 的尾部倒数第 $start 个位置开始检查 $subject。
$length:$subject 中检查的长度。
如果 $length 被设置并且为非负数,那么将从起始位置开始,检查 $subject 的 $length 个长度的字符。
如果 $length 被设置并且为负数,那么将从起始位置开始,直到从 $subject 尾部开始第 $length 个位置截止,对 $subject 进行检查。
【演示程序】
<?php
/**
* strspn ($subject, $mask, $start = null, $length = null)
* **/
$subject = "Hello i am PHP!";
echo strspn($subject, "iHlleoHPP");//搜索整个字符串
echo "<br>";
echo strspn($subject, "iHlleoHPP",5,7);//从第五个字符开始搜索7个字符
echo "<br>";
echo strspn($subject, "iHlleoHPP",-4,4);//从结尾第4个字符开始搜索4个字符
echo "<br>";
echo strspn($subject, "iHlleoHPP",-4,-2);//从结尾第4个字符开始搜索截止于距离结尾2个字符处
?>
【输出结果】
5
0
3
2
【特别注意】
1.该函数是二进制安全的。
2.该函数只返回第一个查找到的连续字符长度。
3.PHP 版本:4+。
4.更新日志:在 PHP 4.3 中,新增了 start 和 length 参数。
【原版定义】
/**
* Finds the length of the initial segment of a string consisting
entirely of characters contained within a given mask.
* @link http://www.php.net/manual/en/function.strspn.php
* @param subject string <p>
* The string to examine.
* </p>
* @param mask string <p>
* The list of allowable characters.
* </p>
* @param start int[optional] <p>
* The position in subject to
* start searching.
* </p>
* <p>
* If start is given and is non-negative,
* then strspn will begin
* examining subject at
* the start'th position. For instance, in
* the string 'abcdef', the character at
* position 0 is 'a', the
* character at position 2 is
* 'c', and so forth.
* </p>
* <p>
* If start is given and is negative,
* then strspn will begin
* examining subject at
* the start'th position from the end
* of subject.
* </p>
* @param length int[optional] <p>
* The length of the segment from subject
* to examine.
* </p>
* <p>
* If length is given and is non-negative,
* then subject will be examined
* for length characters after the starting
* position.
* </p>
* <p>
* If lengthis given and is negative,
* then subject will be examined from the
* starting position up to length
* characters from the end of subject.
* </p>
* @return int the length of the initial segment of subject
* which consists entirely of characters in mask.
*/
转载请注明出处:php1234.cn ,原文地址:http://www.php1234.cn/a/functions/2016/1014/94.html