96 of 410 menu

The mb_stristr Function

The mb_stristr function searches for the first occurrence of a substring in a string without case sensitivity and returns the part of the string from the beginning of the found occurrence to the end. The first parameter is the string to search in, the second is the substring to find, the third (optional) is a boolean value to return the part of the string before the occurrence, and the fourth (optional) is the encoding.

Syntax

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

Example

Find a substring in a string without case sensitivity:

<?php $res = mb_stristr('ABCDE', 'bc'); echo $res; ?>

Execution result:

'BCDE'

Example

Using the $before_needle parameter to get the part of the string before the occurrence:

<?php $res = mb_stristr('ABCDE', 'bc', true); echo $res; ?>

Execution result:

'A'

Example

Search with UTF-8 encoding specified:

<?php $res = mb_stristr('Привет мир', 'МИР', false, 'UTF-8'); echo $res; ?>

Execution result:

'мир'

See Also

  • the mb_strstr function,
    which performs a case-sensitive substring search
  • the stristr function,
    which is similar to mb_stristr but for single-byte encodings
byenru