PHPチュートリアル

PHPホーム PHPイントロ PHPインストール PHP構文 PHPコメント PHP変数 PHPエコー/印刷 PHPデータ型 PHP文字列 PHP番号 PHP数学 PHP定数 PHP演算子 PHP If ... Else ... Elseif PHPスイッチ PHPループ PHP関数 PHP配列 PHPスーパーグローバル PHP正規表現

PHPフォーム

PHPフォーム処理 PHPフォームの検証 PHPフォームが必要 PHPフォームのURL / Eメール PHPフォームの完了

PHP Advanced

PHPの日付と時刻 PHPインクルード PHPファイルの処理 PHPファイルのオープン/読み取り PHPファイルの作成/書き込み PHPファイルのアップロード PHPクッキー PHPセッション PHPフィルター PHPフィルターアドバンスト PHPコールバック関数 PHP JSON PHPの例外

PHPOOP _

PHPOOPとは PHPクラス/オブジェクト PHPコンストラクター PHPデストラクタ PHPアクセス修飾子 PHPの継承 PHP定数 PHP抽象クラス PHPインターフェース PHPの特性 PHP静的メソッド PHPの静的プロパティ PHP名前空間 PHPIterables

MySQLデータベース

MySQLデータベース MySQLコネクト MySQL Create DB MySQLテーブルの作成 MySQLの挿入データ MySQLは最後のIDを取得します MySQL Insert Multiple MySQLを準備しました MySQL Select Data MySQL Where MySQL Order By MySQLデータの削除 MySQLアップデートデータ MySQL制限データ

PHP XML

PHPXMLパーサー PHPSimpleXMLパーサー PHPSimpleXML-取得 PHP XMLExpat PHP XML DOM

PHP -AJAX

AJAXイントロ AJAX PHP AJAXデータベース AJAX XML AJAXライブ検索 AJAXポール

PHPの

PHPの例 PHPコンパイラ PHPクイズ PHP演習 PHP証明書

PHPリファレンス

PHPの概要 PHP配列 PHPカレンダー PHPの日付 PHPディレクトリ PHPエラー PHP例外 PHPファイルシステム PHPフィルター PHP FTP PHP JSON PHPキーワード PHP Libxml PHPメール PHP数学 PHPその他 PHP MySQLi PHPネットワーク PHP出力制御 PHP正規表現 PHP SimpleXML PHPストリーム PHP文字列
addcslashes() addlashes() bin2hex() チョップ() chr() チャンクスプリット() convert_cyr_string() convert_uudecode() convert_uuencode() count_chars() crc32() crypt() エコー() explode() fprint() get_html_translation_table() hebrev() hebrevc() hex2bin() html_entity_decode() htmlentities() htmlspecialchars_decode() htmlspecialchars() implode() 加入() lcfirst() レーベンシュタイン() localeconv() ltrim() md5() md5_file() metaphone() money_format() nl_langinfo() nl2br() number_format() words() parse_str() print() printf() quoted_printable_decode() quoted_printable_encode() quotemeta() rtrim() setlocale() sha1() sha1_file() 類似のテキスト() soundex() sprintf() sscanf() str_getcsv() str_ireplace() str_pad() str_repeat() str_replace() str_rot13() str_shuffle() str_split() str_word_count() strcasecmp() strchr() strcmp() strcoll() strcspn() strip_tags() stripcslashes() stripslashes() 漫画 () ストリップ() strlen() strnatcasecmp() strnatcmp() strncasecmp() strncmp() strpbrk() strpos() strrchr() strrev() strripos() strrpos() strspn() strstr() strtok() strtolower() strtoupper() strtr() substr() substr_compare() substr_count() substr_replace() トリム() ucfirst() ucwords() vfprintf() vprintf() vsprintf() ワードラップ()
PHP変数の処理 PHPXMLパーサー PHP Zip PHPタイムゾーン

PHP substr_count()関数

❮PHP文字列リファレンス

文字列で「world」が発生する回数を数えます。

<?php
echo substr_count("Hello world. The world is nice","world");
?>

substr_count()関数は、文字列内に部分文字列が出現する回数をカウントします。

注:部分文字列では大文字と小文字が区別されます。

注:この関数は、重複するサブストリングをカウントしません(例2を参照)。

注:この関数は、開始パラメーターと長さパラメーターの合計が文字列の長さより大きい場合に警告を生成します(例3を参照)。


構文

substr_count(string,substring,start,length)

パラメータ値

Parameter Description
string Required. Specifies the string to check
substring Required. Specifies the string to search for
start Optional. Specifies where in string to start searching. If negative, it starts counting from the end of the string
length Optional. Specifies the length of the search


技術的な詳細

戻り値: 文字列内に部分文字列が出現する回数を返します
PHPバージョン: 4歳以上
変更ログ: PHP 7.1-長さパラメーターは、0または負の数にすることができます。
PHP7.1-開始パラメーターは負の数にすることができます。
PHP5.1-開始パラメーターと長さ パラメーターが追加されました。

その他の例

すべてのパラメーターの使用:

<?php
$str = "This is nice";
echo strlen($str)."<br>"; // Using strlen() to return the string length
echo substr_count($str,"is")."<br>"; // The number of times "is" occurs in the string
echo substr_count($str,"is",2)."<br>"; // The string is now reduced to "is is nice"
echo substr_count($str,"is",3)."<br>"; // The string is now reduced to "s is nice"
echo substr_count($str,"is",3,3)."<br>"; // The string is now reduced to "s i"
?>

重複した部分文字列:

<?php
$str = "abcabcab";
echo substr_count($str,"abcab"); // This function does not count overlapped substrings
?>

startおよびlengthパラメーターが文字列の長さを超える場合、この関数は警告を出力します。

<?php
echo $str = "This is nice";
substr_count($str,"is",3,9);
?>

長さの値が文字列の長さを超えているため、これは警告を出力します(3 + 9は12より大きい)


❮PHP文字列リファレンス