2024 PHP Conference Japan

substr_count

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

substr_count計算子字串出現的次數

說明

substr_count(
    字串 $haystack,
    字串 $needle,
    整數 $offset = 0,
    ?int $length = null
): int

substr_count() 會回傳子字串 needle 在字串 haystack 中出現的次數。請注意,needle 是區分大小寫的。

注意事項:

此函式不會計算重疊的子字串。請參考以下範例!

參數

haystack

要在其中搜尋的字串

needle

要搜尋的子字串

offset

開始計數的偏移量。如果偏移量為負數,則從字串的末尾開始計數。

length

在指定偏移量之後要搜尋子字串的最大長度。如果偏移量加上長度大於 haystack 的長度,則會輸出警告。負的長度會從 haystack 的末尾開始計算。

回傳值

此函式會回傳一個 int

更新日誌

版本 說明
8.0.0 length 現在可以是 null。
7.1.0 已新增對負數 offsetlength 的支援。length 現在也可以是 0

範例

範例 #1 substr_count() 範例

<?php
$text
= 'This is a test';
echo
strlen($text); // 14

echo substr_count($text, 'is'); // 2

// 字串縮減為 's is a test',因此印出 1
echo substr_count($text, 'is', 3);

// 字串縮減為 's i',因此印出 0
echo substr_count($text, 'is', 3, 3);

// 產生警告,因為 5+10 > 14
echo substr_count($text, 'is', 5, 10);


// 只印出 1,因為它不計算重疊的子字串
$text2 = 'gcdgcdgcd';
echo
substr_count($text2, 'gcdgcd');
?>

另請參閱

  • count_chars() - 返回字串中使用的字元資訊
  • strpos() - 尋找子字串在字串中第一次出現的位置
  • substr() - 返回字串的一部分
  • strstr() - 尋找字串第一次出現的位置

新增筆記

使用者貢獻的筆記 7 則筆記

tuxedobob
8 年前
值得注意的是,這個函式的速度驚人地快。我第一次在我們的網頁伺服器上用一個大約 500KB 的字串來測試它。它在 0.0000 秒內找到了我要尋找的字串出現的 6 個位置。是的,它的執行速度比 microtime() 能測量的還要快。

為了給它一個挑戰,我接著在 2010 年的 Mac 筆記型電腦上用一個 120.5MB 的字串來測試它。對於一個測試字串,它在 0.0266 秒內找到了 2385 個出現的位置。另一個測試字串則在 0.114 秒內找到了 290 個出現的位置。

長話短說,如果您想知道這個函式是否會降低您的腳本速度,答案可能是不會。
flobi at flobi dot com
18 年前
對於任何需要不區分大小寫的人來說,要做到這一點很容易。只需將 haystack 和 needle 轉換為相同的大小寫(大寫或小寫)。

substr_count(strtoupper($haystack), strtoupper($needle))
tweston at bangordailynews dot com
9 年前
為了考慮 jrhodes 指出的情況,我們可以將程式碼改為

substr_count ( implode( ',', $haystackArray ), $needle );

這樣一來

array (
0 => "mystringth",
1 => "atislong"
);

)

會變成

mystringth,atislong
這會使 $needle = "that" 的計數再次變為 0。
15 年前

有人建議使用

substr_count ( implode( $haystackArray ), $needle );

array (
0 => "mystringth",
1 => "atislong"
);

來取代先前描述的函式,然而這有一個缺陷。例如,這個陣列
如果您要計算 "that",implode 版本會返回 1,但先前描述的函式會返回 0。
17 年前
<?php

function substr_count_array( $haystack, $needle ) {
$count = 0;
foreach (
$needle as $substring) {
$count += substr_count( $haystack, $substring);
}
return
$count;
}
?>
XinfoX X at X XkarlX X-X XphilippX X dot X XdeX
20 年前
另一個由 "chris at pecoraro dot net" 發佈的 "cgcgcgcgcgcgc" 例子的參考。

您的需求可以使用 Perl 相容的正規表示式及其前瞻和後顧功能來滿足。

範例

$number_of_full_pattern = preg_match_all('/(cgc)/', "cgcgcgcgcgcgcg", $chunks);

其作用類似於 substr_count 函式。變數 $number_of_full_pattern 的值為 3,因為 Perl 相容正規表示式的預設行為是消耗掉與(子)模式匹配的字串主體字元。也就是說,指標會移動到匹配子字串的末尾。
但我們可以使用前瞻功能來停用指標的移動

$number_of_full_pattern = preg_match_all('/(cg(?=c))/', "cgcgcgcgcgcgcg", $chunks);

在這種情況下,變數 $number_of_full_pattern 的值為 6。
首先會匹配字串 "cg",並且指標會移動到此字串的末尾。然後正規表示式會向前查看是否可以匹配 'c'。儘管出現了字元 'c',但指標並不會移動。
php at blink dot at
10 年前
這將處理一個字串,其中不知道逗號或句點是用作千位分隔符號還是小數點分隔符號。只有當只有一個逗號或句點和 3 個可能的小數位數時 (123.456 或 123,456),才會導致衝突。傳遞一個可選參數來處理這種情況(假設為千位分隔符號、假設為小數點分隔符號、句點為小數點分隔符號、逗號為小數點分隔符號)。它假設輸入字串採用以下任何格式。

function toFloat($pString, $seperatorOnConflict="f")
{
$decSeperator=".";
$thSeperator="";

$pString=str_replace(" ", $thSeperator, $pString);

$firstPeriod=strpos($pString, ".");
$firstComma=strpos($pString, ",");
if($firstPeriod!==FALSE && $firstComma!==FALSE) {
if($firstPeriod<$firstComma) {
$pString=str_replace(".", $thSeperator, $pString);
$pString=str_replace(",", $decSeperator, $pString);
}
} else {
$pString=str_replace(",", $thSeperator, $pString);
}
}
} else if($firstPeriod!==FALSE || $firstComma!==FALSE) {
$seperator=$firstPeriod!==FALSE?".":",";
if(substr_count($pString, $seperator)==1) {
$lastPeriodOrComma=strpos($pString, $seperator);
if($lastPeriodOrComma==(strlen($pString)-4) && ($seperatorOnConflict!=$seperator && $seperatorOnConflict!="f")) {
$pString=str_replace($seperator, $thSeperator, $pString);
}
} else {
} else {
}
}
} else {
$pString=str_replace($seperator, $thSeperator, $pString);
}
}
return(float)$pString;
}

function testFloatParsing() {
$floatvals = array(
"22 000",
"22,000",
"22.000",
"123 456",
"123,456",
"123.456",
"22 000,76",
"22.000,76",
"22,000.76",
"22000.76",
"22000,76",
"1.022.000,76",
"1,022,000.76",
"1,000,000",
"1.000.000",
"1022000.76",
"1022000,76",
"1022000",
"0.76",
"0,76",
"0.00",
"0,00",
"1.00",
"1,00",
"-22 000,76",
"-22.000,76",
"-22,000.76",
"-22 000",
"-22,000",
"-22.000",
"-22000.76",
"-22000,76",
"-1.022.000,76",
"-1,022,000.76",
"-1,000,000",
"-1.000.000",
"-1022000.76",
"-1022000,76",
"-1022000",
"-0.76",
"-0,76",
"-0.00",
"-0,00",
"-1.00",
"-1,00"
);

echo "<table>";
echo "<tr>";
echo "<th>字串</th>";
echo "<th>千位分隔</th>";
echo "<th>小數部分</th>";
echo "<th>句點為小數點</th>";
echo "<th>逗號為小數點</th>";
echo "</tr>";

foreach ($floatvals as $fval) {
echo "<tr>";
echo "<td>" . (string) $fval . "</td>";

echo "<td>" . (float) toFloat($fval, "") . "</td>";
echo "<td>" . (float) toFloat($fval, "f") . "</td>";
echo "<td>" . (float) toFloat($fval, ".") . "</td>";
echo "<td>" . (float) toFloat($fval, ",") . "</td>";
echo "</tr>";
}
輸出 "</table>";
}
To Top