1. int strspn (string $str1 , string $str2 [, int $start [, int $length ]])
返回$str1中连续匹配$str2的长度,$start用来指定开始位置,$length用来指定搜索长度,如
$pos = strspn("4232987 is my qq card!","012345689");将返回7
2. int strcspn ( string $str1 , string $str2 [,int $start [, int $length]])
返回$str1中连续不匹配$str2的长度,$start用来指定开始位置,$length用来指定搜索长度,
如 $cpos = strcspn("my qq card is 4232987!","012345689");将返回14
两者结合可以用来截取字符串,如
$a = "aedfetgilciocwojnsiedewlijloewskisiwdsseidl";
$b = "abcd";
function diff($a,$b)
{
static $str="";
$start = strspn($a,$b);
$end = strcspn($a,$b,$start);
$str .= substr($a,$start,$end);
$a = substr($a,$start+$end);
if(!empty($a))
diff($a,$b);
return $str;
}
echo "the result is: " . diff($a,$b);//efetgiliowojnsieewlijloewskisiwsseil
由此可见diff函数的作用是把a中所有的b字符全去掉后的结果.
int strspn (string $str1 , string $str2 [, int $start [, int $length ]])
该函数的作用是取$str2中长度为$length的字符串(用$s代替这个字符串)。如果在字 符$str1中,从位置$start 开始(第1个是0,跟数组的索引一致),连续$n个字符在$s中。返回的就是$n。如果$start和$length没有值,默 认$start=0,$length是$str2的长度。
ex.
$a = 'abcdefghijklmnopqrst';
$b1 = 'abcd';
$b2 = 'efg';
$b3 = 'acde';
$c1 = strspn($a,$b1); // 4
$c2 = strspn($a,$b2); // 0
$c3 = strspn($a,$b3); // 1
$c4 = strspn($a,$b2,4); //3
$c5 = strspn($a,$b2,4,2); //2
$c6 = strspn($a,$b2,5,3); //2
strcspn 各参数同strspn。只是返回的结果是$str1中不属于$str2中的结果
$a = 'abcdefghijklmnopqrst';
$b1 = 'abcd';
$b2 = 'efg';
$b3 = 'acde';
$c1 = strcspn($a,$b1); // 0
$c2 = strcspn($a,$b2); // 4
$c3 = strcspn($a,$b3); // 0
$c4 = strcspn($a,$b2,4); //0
$c5 = strcspn($a,$b2,4,2); //0
$c6 = strcspn($a,$b2,5,3); //0