strcspn()函数是PHP中的一个内置函数,它返回要搜索的指定字符的任何部分之前字符串中存在的字符数。此函数区分大小写。
null
语法:
strcspn( $string, $charlist, $start, $length)
参数: 此函数接受上述语法中所示的四个参数。前两个参数是必需的,必须提供,而其余两个参数是可选的。所有这些参数如下所述:
- $string: 此必填参数指定要搜索的字符串
- $charlist: 此必填参数指定要在给定的$字符串中搜索的字符列表。
- $start: 此可选参数指定从何处开始搜索字符串的索引。
- 如果给出了$start并且 非负 ,然后strcspn()将从该位置开始检查$string。
- 如果给出了$start并且 消极的 ,然后strcspn()将从$string结尾的那个位置开始检查$string。
- $length: 它指定需要搜索的$string的字符数。它的默认值是直到$string结束。
- 如果给出了$length并且 非负 ,然后将从起始位置检查$string中的$length字符。
- 如果给出了$length并且 消极的 ,然后将从起始位置检查$string,从$string的结尾检查长度为$length的字符。
返回值: 返回字符串中任何字符之前的起始位置(包括空格)的字符数 $charlist 参数在字符串中找到。
例如:
Input : $string = "Geeks for Geeks", $charlist = "mnopqr"Output : 7Input : $string = "Geeks for Geeks", $charlist = "for"Output : 6
下面的程序将说明strcspn()函数的用法:
项目1: 这个程序展示了strcspn()函数的简单用法。
PHP
<?php // Output is 6 because the input string // contains 6 characters "Geeks " before // the first character 'f' from the list // "for" is found in the string. echo strcspn ( "Geeks for Geeks" , "for" ); ?> |
输出 :
6
项目2: 这个程序显示strcspn()函数的大小写敏感度。
PHP
<?php // Output is 7 because the input string // does not contain 'F' as specified in the list "For". // Hence the first character from the // list that is present in the string is 'o' echo strcspn ( "Geeks for Geeks" , "For" ); ?> |
输出 :
7
方案3: 这个程序显示了带$start参数的strcspn()函数的用法。
PHP
<?php // Searches from index 5 till // the end of the string echo strcspn ( "Geeks for Geeks" , "G" , 5); ?> |
输出 :
5
方案4: 这个程序演示了带负$length参数的strcspn()函数的用法。
PHP
<?php // Searches from index 5 till 5-th position // from end. Output is 0 since the character // at $start (i.e. 5) is present in the // specified list of characters echo strcspn ( "Geeks for Geeks" , " for" , 5, -5); ?> |
输出 :
0
方案5: 这个程序显示了带负$start参数的strcspn()函数的用法。
php
<?php // Searches from 5th index from the end of the string // Output is 0 as the character 'G' in the // specified starting index is present in the // given list of characters to be checked. echo strcspn ( "Geeks for Geeks" , "Geek" , -5); ?> |
输出 :
0
参考: http://php.net/manual/en/function.strcspn.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END