PHP 字符串 strcspn() 函數
strcspn() 是 PHP 的預定義函數。愛掏網 - it200.com它用于找到字符串中初始段的長度,或返回包括空格在內的字符數量。愛掏網 - it200.com
注意:此函數是二進制安全的。愛掏網 - it200.com
strcspn(string,char,start,length);
| 參數 | 描述 | 必需/可選 |
|---|---|---|
| string | 指定要搜索的字符串。愛掏網 - it200.com | 必需 |
| char | 指定要搜索的字符。愛掏網 - it200.com | 必需 |
| start | 指定在字符串中開始的位置。愛掏網 - it200.com | 可選 |
| length | 指定字符串的長度。愛掏網 - it200.com | 可選 |
示例1
<?php
// count all character with whitespace
str="Hello Javatpoint";
echo "Your string is: ".str;
echo "<br>";
echo "By using strcspn() function:".strcspn($str,"w");
?>
輸出:
Your string is: Hello Javatpoint
By using strcspn() function:16
示例2
<?php
//The start position is 0 and the length of the search string is 5.
str="Hello Javatpoint";
echo "Your string is: ".str;
echo "<br>";
echo "By using strcspn() function:".strcspn($str,"w","0","5");
?>
輸出:
Your string is: Hello Javatpoint
By using strcspn() function:5
示例3
<?php
a = strcspn('abcd', 'apple');
var_dump(a);
?>
輸出:
int(0)
示例4
<?php
str = strcspn('abcd', 'banana');
var_dump(str);
?>
輸出:
int(0)
示例5
<?php
str = strcspn('hello', 'l');
var_dump(str);
?>
輸出:
int(2)
示例6
<?php
str = strcspn('hello', 'world');
var_dump(str);
?>
輸出:
int(2)
示例7
<?php
str = strcspn('abcdhelloabcd', 'abcd', -9);
var_dump(str);
?>
輸出:
int(5)
示例8
<?php
str = strcspn('abcdhelloabcd', 'abcd', -9, -5);
var_dump(str);
?>
輸出:
int(4)
參考文獻:
http://php.net/manual/en/function.strcspn.php
聲明:所有內容來自互聯網搜索結果,不保證100%準確性,僅供參考。如若本站內容侵犯了原著者的合法權益,可聯系我們進行處理。