mb_stristr

(PHP 5 >= 5.2.0, PHP 7, PHP 8)

mb_stristr 大文字小文字を区別せず、 文字列の中で指定した文字列が最初に現れる位置を探す

説明

mb_stristr(
    string $haystack,
    string $needle,
    bool $before_needle = false,
    ?string $encoding = null
): string|false

mb_stristr() は、 haystack の中で最初に needle が現れる場所を探し、 haystack の部分文字列を返します。 mb_strstr() とは異なり、 mb_stristr() は大文字小文字を区別しません。 needle が見つからなかった場合は false を返します。

パラメータ

haystack

needle が最初に現れる位置を見つける文字列。

needle

haystack の中で探す文字列。

before_needle

この関数が haystack のどの部分を返すのかを指定します。 true の場合は、haystack の先頭から needle が最初に現れる位置までを返します (needle を含めません)。 false の場合は、needle が最初に現れる位置から haystack の最後までを返します (needle を含めます)。

encoding

使用する文字エンコーディング名。 省略した場合は内部文字エンコーディングが用いられます。

戻り値

haystack の部分文字列を返します。 needle が見つからない場合は false を返します。

変更履歴

バージョン 説明
8.0.0 needle は、空の文字列も受け入れるようになりました。
8.0.0 encoding は、nullable になりました。

参考

  • stristr() - 大文字小文字を区別しない strstr
  • strstr() - 文字列が最初に現れる位置を見つける
  • mb_strstr() - 文字列の中で、指定した文字列が最初に現れる位置を見つける

add a note

User Contributed Notes 1 note

up
0
nowfel dot terki at mailfence dot com
1 year ago
Be aware that if needle is an empty string, mb_stristr return the haystack by default.

For exemple:

<?php
if (mb_stristr("foo", "")) {
echo
"We enter in condition";
}
?>

Because in the above exemple the return of mb_stristr is "foo".

So if we do not want this kind of behaviour, we must set the third argument, ($before_needle) to true.

<?php
if (mb_stristr("foo", "", true)) {
echo
"We do not enter in condition";
}
?>

It can be useful to know it, specially when needle is dynamic.
To Top