PHP Conference Japan 2024

strspn

(PHP 4, PHP 5, PHP 7, PHP 8)

strspn 尋找字串的初始區段長度,該區段完全由給定遮罩中包含的字元組成

說明

strspn(
    字串 $string,
    字串 $characters,
    整數 $offset = 0,
    ?整數 $length = null
): 整數

尋找 string 的起始區段長度,該區段*僅*包含來自 characters 的字元。

如果省略 offsetlength,則會檢查整個 string。如果包含它們,則效果與呼叫 strspn(substr($string, $offset, $length), $characters) 相同(有關更多資訊,請參閱 substr)。

以下程式碼

<?php
$var
= strspn("42 is the answer to the 128th question.", "1234567890");
?>
會將 2 指派給 $var,因為字串 "42" 是 string 的起始區段,且僅由 "1234567890" 中包含的字元組成。

參數

string

要檢查的字串。

characters

允許的字元列表。

offset

開始搜尋的 string 中的位置。

如果給定 offset 且為非負數,則 strspn() 將從 string 的第 offset 個位置開始檢查。例如,在字串 'abcdef' 中,位置 0 的字元是 'a',位置 2 的字元是 'c',依此類推。

如果給定 offset 且為負數,則 strspn() 將從 string 尾端算起的第 offset 個位置開始檢查。

length

要檢查的 string 區段的長度。

如果給定 length 且為非負數,則將從起始位置開始檢查 stringlength 個字元。

如果給定 length 且為負數,則將從起始位置開始檢查 string,直到從 string 尾端算起的 length 個字元。

傳回值

傳回 string 的起始區段長度,該區段完全由 characters 中的字元組成。

注意事項:

設定 offset 參數時,傳回的長度是從這個位置開始計算,而不是從 string 的開頭開始計算。

更新日誌

版本 說明
8.0.0 length 現在可以為 null。

範例

範例 #1 strspn() 範例

<?php
// 主題字串不是以任何來自遮罩字串的字元開頭
var_dump(strspn("foo", "o"));

// 從偏移量 1 開始檢查主題字串的兩個字元
var_dump(strspn("foo", "o", 1, 2));

// 從偏移量 1 開始檢查主題字串的一個字元
var_dump(strspn("foo", "o", 1, 1));
?>

以上範例將輸出

int(0)
int(2)
int(1)

註釋

注意這個函式是二進位安全的。

另請參閱

  • strcspn() - 尋找與遮罩不匹配的初始區段長度

新增註解

使用者貢獻的註解 8 則註解

barry dot balkowski at gmail dot com
16 年前
我花了一些時間才理解這個函式的運作方式…
我用自己的話編寫了我自己的解釋,這對我個人來說比官方的解釋或網路上不同教學中的解釋更容易理解。
或許,這可以為其他人節省幾分鐘…

<?php
strspn
(string $haystack, string $char_list [, int $start [, int $length]])
?>

運作方式
- 搜尋 $haystack 中完全由第二個參數提供的字元組成的區段
- $haystack 必須以 $char_list 中提供的其中一個字元開頭,否則函式將找不到任何內容
- 一旦函式遇到未在 $chars 中提到的字元,它就會理解區段已結束並停止(它不會搜尋第二個、第三個等等區段)
- 最後,它會測量區段的長度並回傳它(即長度)

換句話說,它會在字串中找到一個完全由 $chars_list 中提供的字元組成的區段(僅第一個),並回傳其長度
AT-HE (at_he.hotmail)
14 年前
您可以將此函式與 strlen 一起使用來檢查非法字元,字串長度必須與 strspn 相同(我的字串中包含在另一個字串中的字元)

<?php

$digits
='0123456789';

if (
strlen($phone) != strspn($phone,$digits))
echo
"非法字元";

?>
B Crawford
17 年前
與等效的 preg_match() 方法相比,此函式在檢查非法字元方面明顯更快。
mrsohailkhan at gmail dot com
13 年前
很難直接從定義中理解,當我搜尋它時,我才知道

strspn() 會告訴您一個完全由接受集合中的字元組成的字串的長度。也就是說,它會開始向下遍歷 str,直到找到一個不在集合中的字元(也就是一個不被接受的字元),然後回傳到目前為止的字串長度。

以及

strcspn() 的工作方式大致相同,不同之處在於它會向下遍歷 str,直到找到拒絕集合中的一個字元(也就是一個要被拒絕的字元)。然後它會回傳到目前為止的字串長度。

<?php
$acceptSet
= "aeiou";
$rejectSet = "y";

$str1 ="a banana";
$str2 ="the bolivian navy on manuvers in the south pacific";

echo
$n = strspn($str1,$acceptSet);// $n == 1,只匹配到 "a"

echo $n = strcspn($str2,$rejectSet);// n = 16,匹配到 "the bolivian nav"
?>

希望這個例子能幫助理解 strspn() 和 strcspn() 的概念。
Dmitry Mazur
15 年前
第二個參數是一組允許的字元。
strspn 會返回第一個不允許字元的零起始索引。
bob at example dot com
12 年前
快速檢查字串是否完全由遮罩中的字元組成的的方法是將 strspn 與 strlen 進行比較,例如:

<?php
$path
= $_SERVER['PATH_INFO'];
if (
strspn($path,'/') == strlen($path)) {
//PATH_INFO 為空
}
?>
lincoln dot mahmud at gmail dot com
4 年前
取得群組匹配字母

<?php

$s
= 'aaabbbcceeffaaeeeaaabbzmmm';

function
groupby( $s ){
static
$a = [];
static
$i = 0;

$o = strspn( $s, $s[$i], $i);
$a[ $i ] = [ $s[$i] => $o ];
$i += $o;

if(
$i < strlen($s) ) {
groupby($s);
}

return
$a;
}

print_r(groupby($s));

?>
nino dot skopac at gmail dot com
6 年前
strspn 和 preg_match 在驗證數字方面速度似乎一樣快

<?php

$testValInvalid
= 'foobar123^^';
$testValValid = '12346';
$allowedChars = '1234567890';

$t1 = microtime(true);
for (
$i = 0; $i < 1000000; $i++) {
assert
(strspn($testValInvalid, $allowedChars) != strlen($testValInvalid));
assert
(strspn($testValValid, $allowedChars) == strlen($testValValid));
}
print
'使用 strspn 耗時:' . (microtime(true) - $t1);
print
PHP_EOL;

$t1 = microtime(true);
for (
$i = 0; $i < 1000000; $i++) {
assert
(preg_match('/^[0-9]+$/', $testValInvalid) === 0);
assert
(preg_match('/^[0-9]+$/', $testValValid));
}

print
'使用 preg_match 耗時:' . (microtime(true) - $t1);
print
PHP_EOL;

/**
nino-mcb:hosp_web ninoskopac$ php test.php
使用 strspn 耗時:3.24165391922
使用 preg_match 耗時:3.1820080280304
nino-mcb:hosp_web ninoskopac$ php test.php
使用 strspn 耗時:3.1806418895721
使用 preg_match 耗時:3.2244551181793
*/
?>
To Top