PHP strstr()関数
例
「Helloworld!」内で「world」の最初の出現を見つけます。文字列の残りの部分を返します。
<?php
echo strstr("Hello world!","world");
?>
定義と使用法
strstr()関数は、別の文字列内で最初に出現する文字列を検索します。
注:この関数はバイナリセーフです。
注:この関数では大文字と小文字が区別されます。大文字と小文字を区別しない検索には、stristr()関数を使用します。
構文
strstr(string,search,before_search)
パラメータ値
Parameter | Description |
---|---|
string | Required. Specifies the string to search |
search | Required. Specifies the string to search for. If this parameter is a number, it will search for the character matching the ASCII value of the number |
before_search | Optional. A boolean value whose default is "false". If set to "true", it returns the part of the string before the first occurrence of the search parameter. |
技術的な詳細
戻り値: | 残りの文字列(一致点から)を返します。検索する文字列が見つからない場合はFALSEを返します。 |
---|---|
PHPバージョン: | 4歳以上 |
変更ログ: | before_searchパラメーターはPHP5.3で追加されました |
その他の例
例
文字列で「o」のASCII値を検索し、残りの文字列を返します。
<?php
echo strstr("Hello world!",111);
?>
例
「world」が最初に出現する前の文字列の部分を返します。
<?php
echo strstr("Hello world!","world",true);
?>
❮PHP文字列リファレンス