substr_countとは? わかりやすく解説

Weblio 辞書 > コンピュータ > PHP関数リファレンス > substr_countの意味・解説 

substr_count

(PHP 4, PHP 5)
substr_count — 副文字列の出現回数を数える

説明

int substr_count ( string haystack, string needle [, int offset [, int length]] )
substr_count() は、文字列 haystack の中での副文字列 needle の出現回数を返します。 needle は英大小文字を区別することに注意してください。
注意: この関数は重なり合う副文字列をカウントしません。以下の例を見てください !

パラメータ

haystack
検索対象の文字列
needle
検索する副文字列
offset
開始位置のオフセット
length
指定したオフセット以降に副文字列で検索する最大長。 オフセットと長さの総和が haystack の長さよりも長い場合、警告が発生します。

返り値

この関数は 整数 を返します。

変更履歴

バージョン説明
5.1.0 offsetlength パラメータが追加されました。

例 2328. substr_count() の例
<?php
$text = 'This is a test';
echo strlen($text); // 14

echo substr_count($text, 'is'); // 2

// 文字列は 's is a test' になっているので, 1 が表示される
echo substr_count($text, 'is', 3);

// テキストは 's i' になっているので, 0 が表示される
echo substr_count($text, 'is', 3, 3);

// 5+10 > 14 なので、警告が発生する
echo substr_count($text, 'is', 5, 10);


// 重なっている副文字列はカウントされないので、1 が表示される
$text2 = 'gcdgcdgcd';
echo substr_count($text2, 'gcdgcd');
?>


参考

count_chars()
strpos()
substr()
strstr()




英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「substr_count」の関連用語

substr_countのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



substr_countのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
PHP Documentation GroupPHP Documentation Group
Copyright © 1997 - 2024 by the PHP Documentation Group.

©2024 GRAS Group, Inc.RSS