溫馨提示×

php中substr_count函數怎么使用

PHP
小億
105
2024-06-12 17:25:06
欄目: 編程語言

substr_count函數用于計算一個字符串中某個子字符串出現的次數。

語法: int substr_count ( string $haystack , string $needle [, int $offset = 0 [, int $length ]] )

參數:

  • $haystack:要搜索的主字符串。
  • $needle:要查找的子字符串。
  • $offset:可選參數,指定開始搜索的位置。
  • $length:可選參數,指定搜索的長度。

示例:

$str = "Hello, World! Hello, PHP!";
$count = substr_count($str, "Hello");
echo $count; // 輸出:2

上面的示例中,substr_count函數會在字符串$str中查找子字符串"Hello"出現的次數,并將結果存儲在$count變量中。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女