PHPのお勉強!

PHP TOP

stristr

(PHP 4, PHP 5, PHP 7, PHP 8)

stristr大文字小文字を区別しない strstr()

説明

stristr(string $haystack, string $needle, bool $before_needle = false): string|false

haystack において needle が最初に見つかった位置を含めてそこから最後までを返します。

パラメータ

haystack

検索対象の文字列。

needle

検索する文字列。

PHP 8.0.0 より前のバージョンでは、needle が文字列でない場合、 数値に変換され、文字の通常の値として扱われていました。 この振る舞いは PHP 7.3.0 以降では推奨されないので、 この機能を使用しないことを強く推奨します。 意図した動作に依存する場合、 needle を string に明示的にキャストするか、 明示的に chr() 関数を呼び出すべきでしょう。

before_needle

true にすると、stristr() の戻り値は、haystack の中で最初に needle があらわれる箇所より前の部分となります (needle は含めません)。

needle および haystack は大文字小文字を区別せずに評価されます。

戻り値

マッチした部分文字列を返します。needle が見つからない場合は false を返します。

変更履歴

バージョン 説明
8.2.0 ケースフォールディングは、setlocale() で設定されたロケールに依存しなくなりました。 ASCII のケースフォールディングのみが行われます。 ASCII でないバイト列は、バイト値として比較されます。
8.0.0 needle は、空文字列を受け入れるようになりました。
8.0.0 needle に数値を渡すことはサポートされなくなりました。
7.3.0 needle に数値を渡すことは非推奨になりました。

例1 stristr() の例

<?php
$email
= 'USER@EXAMPLE.com';
echo
stristr($email, 'e'); // 出力は ER@EXAMPLE.com となります
echo stristr($email, 'e', true); // 出力は US となります
?>

例2 文字列が見つかるかどうかをテストする

<?php
$string
= 'Hello World!';
if(
stristr($string, 'earth') === FALSE) {
echo
'"earth" not found in string';
}
// 出力は "earth" not found in string となります
?>

例3 文字列以外の needle の指定

<?php
$string
= 'APPLE';
echo
stristr($string, 97); // 97 = 小文字の a
// 出力は APPLE となります
?>

注意

注意: この関数はバイナリデータに対応しています。

参考

  • strstr() - 文字列が最初に現れる位置を見つける
  • strrchr() - 文字列中に文字が最後に現れる場所を取得する
  • stripos() - 大文字小文字を区別せずに文字列が最初に現れる位置を探す
  • strpbrk() - 文字列の中から任意の文字を探す
  • preg_match() - 正規表現によるマッチングを行う

add a note

User Contributed Notes 5 notes

up
17
dpatton.at.confluence.org
22 years ago
There was a change in PHP 4.2.3 that can cause a warning message
to be generated when using stristr(), even though no message was
generated in older versions of PHP.

The following will generate a warning message in 4.0.6 and 4.2.3:
stristr("haystack", "");
OR
$needle = ""; stristr("haystack", $needle);

This will _not_ generate an "Empty Delimiter" warning message in
4.0.6, but _will_ in 4.2.3:
unset($needle); stristr("haystack", $needle);

Here's a URL that documents what was changed:
http://groups.google.ca/groups?selm=cvshholzgra1031224321%40cvsserver
up
7
giz at gbdesign dot net
17 years ago
Just been caught out by stristr trying to converting the needle from an Int to an ASCII value.

Got round this by casting the value to a string.

<?php
if( !stristr( $file, (string) $myCustomer->getCustomerID() ) ) {
// Permission denied
}
?>
up
3
Techdeck at Techdeck dot org
21 years ago
An example for the stristr() function:

<?php
$a
= "I like php";
if (
stristr("$a", "LikE PhP")) {
print (
"According to \$a, you like PHP.");
}
?>

It will look in $a for "like php" (NOT case sensetive. though, strstr() is case-sensetive).

For the ones of you who uses linux.. It is similiar to the "grep" command.
Actually.. "grep -i".
up
-1
notepad at codewalkers dot com
19 years ago
<?php

function stristr_reverse($haystack, $needle) {
$pos = stripos($haystack, $needle) + strlen($needle);
return
substr($haystack, 0, $pos);
}
$email = 'USER@EXAMPLE.com';
echo
stristr_reverse($email, 'er');
// outputs USER

?>
up
-3
jukka
10 years ago
I think there is a bug in php 5.3 in stristr with uppercase Ä containing other character

http://pastebin.com/5bP6uztY

if you search only with täry it works, but as soon as the word is tärylä it does not. TÄRYL works fine