strpos

(PHP 3, PHP 4 , PHP 5)

strpos --  文字列が最初に現れる場所を見つける

説明

int strpos ( string haystack, string needle [, int offset])

文字列 haystack の中で、 needleが最初に現れた位置を数字で返します。 strrpos()とは異なり、この関数は needle パラメータとして文字列全体をとり、そ の文字列全体が検索対象となります。

needleが見つからない場合、 strpos()boolean FALSEを返しま す。

警告

この関数は論理値 FALSEを返す可能性がありますが、FALSEとして評価される 0や""といった値を返す可能性もあります。 詳細については論理値の 章を参照してください。この関数の返り値を調べるには ===演算子を 使用して下さい。

例 1. strpos() の例

<?php
$mystring
= 'abc';
$findme   = 'a';
$pos = strpos($mystring, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos === false) {
    echo
"The string '$findme' was not found in the string '$mystring'";
} else {
    echo
"The string '$findme' was found in the string '$mystring'";
    echo
" and exists at position $pos";
}

?>

needleが文字列でない場合、 整数に変換され、文字が並んだ値として適用されます。

オプションのパラメータoffsetにより、 検索を開始するhaystackの文字を指定することが できます。この場合でも返される位置は、haystack の先頭からの相対位置となります。

strrpos(), stripos(), strrchr(), substr(), stristr(), strstr()も参照して 下さい。