函式名稱:mb_substr_count()
函式描述:mb_substr_count() 函式用於計算一個字串在另一個字串中出現的次數,不區分大小寫。
函式用法: mb_substr_count(string $haystack, string $needle, string|null $encoding = null): int
引數:
- $haystack:必需,要搜尋的字串。
- $needle:必需,要搜尋的子字串。
- $encoding:可選,指定字元編碼。預設為內部字元編碼。
返回值: 返回子字串在字串中出現的次數。
示例:
$text = "Hello, I'm a PHP developer.";
$substring = "php";
$count = mb_substr_count(mb_strtolower($text), mb_strtolower($substring));
echo "子字串出現的次數為: " . $count;
輸出: 子字串出現的次數為: 1
說明: 上述示例中,我們使用了 mb_substr_count() 函式來計運算元字串 "php" 在字串 "Hello, I'm a PHP developer." 中出現的次數。由於該函式不區分大小寫,我們在計算之前使用了 mb_strtolower() 函式將字串轉換為小寫。最後,我們將計算結果輸出為 "子字串出現的次數為:1"。
熱門工具排行榜