mb_substr_count
計算子字符串出現次數
函數名稱:mb_substr_count()
函數描述:mb_substr_count() 函數用於計算一個字符串在另一個字符串中出現的次數,不區分大小寫。
函數用法: mb_substr_count(string $haystack, string $needle, string|null $encoding = null): int
參數:
返回值: 返回子字符串在字符串中出現的次數。
示例:
$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"。