どうも、ちょげ(@chogetarou)です。
文字列(string)の特定の文字をカウントする方法を紹介します。
方法
文字列(string)の特定の文字をカウントするには、substr_count()を使います。
まず、substr_count()を呼び出します。
そして、substr_count()の第1引数に対象の文字列、第2引数にカウントする文字を指定します。
//text=対象の文字列, char=カウントする文字
$result = substr_count(text, char);
上記のsubstr_count()は、第1引数の文字列(string)の第2引数の文字をカウントし、その個数を取得します。
使用例
<?php
$text = "Hello, World";
$result = substr_count($text, 'l');
echo $result
?>
出力:
3
コメント