mb_substr_count

(PHP 4 >= 4.3.0, PHP 5, PHP 7, PHP 8)

mb_substr_count — 部分文字列の出現回数を数える

説明

function mb_substr_count(string $haystack, string $needle, ?string $encoding = null): int

文字列 haystack の中での部分文字列 needle の出現回数を数えます。

パラメータ

haystack

調べたい文字列。

needle

見つける文字列。

encoding

encoding パラメータには文字エンコーディングを指定します。省略した場合、もしくは null の場合は、 内部文字エンコーディングを使用します。

戻り値

文字列 haystack の中での部分文字列 needle の出現回数を返します。

変更履歴

バージョン 説明
8.0.0 encoding は、nullable になりました。

例

例1 mb_substr_count() の例

<?php
echo mb_substr_count("これはてすとです。", "す"); // 2 を出力する
?>

参考

  • mb_strpos() - 文字列の中に指定した文字列が最初に現れる位置を見つける
  • mb_substr() - 文字列の一部を得る
  • substr_count() - 副文字列の出現回数を数える
+add a note

User Contributed Notes

There are no user contributed notes for this page.