PHP strcspn() 函數

PHP String 參考手冊 PHP String 參考手冊

實例

輸出在字串 "Hello world!" 中找到字元 "w" 之前查找的字元數:

<?php
echo strcspn("Hello world!","w");
?>


定義和用法

strcspn() 函數返回在找到任何指定的字元之前,在字串查找的字元數(包括空格)。

提示: Use the strspn() function to the number of characters found in the string that contains only characters from a specified character list.

注釋: This function is binary-safe.


語法

strcspn(string,char,start,length)

參數 描述
string 必需。規定要搜索的字串。
char 必需。規定要查找的字元。
start 可選。規定開始查找的位置。
length 可選。規定字串的長度(搜索多少字元)。

技術細節

返回值: 返回在找到任何指定的字元之前,在字串查找的字元數。
PHP 版本: 4+
更新日誌: 在 PHP 4.3 中,新增了 startlength 參數。


更多實例

實例 1

使用所有的參數來輸出在字串 "Hello world!" 中找到字元 "w" 之前查找的字元數:

<?php
echo strcspn("Hello world!","w",0,6); // The start position is 0 and the length of the search string is 6.
?>



PHP String 參考手冊 PHP String 參考手冊