102 of 410 menu

The mb_strstr Function

The mb_strstr function searches for the first occurrence of a substring in a string with multibyte encoding support. The first parameter is the string to search in. The second parameter is the substring to search for. The third optional parameter determines whether to return the part of the string before the found occurrence. The fourth optional parameter specifies the encoding.

Syntax

mb_strstr( string $haystack, string $needle, [bool $before_needle = false], [string $encoding], );

Example

Find the first occurrence of a substring in a string:

<?php $res = mb_strstr('abcde', 'b'); echo $res; ?>

Code execution result:

'bcde'

Example

Find the first occurrence of a substring in a string and return the part before the occurrence:

<?php $res = mb_strstr('abcde', 'cd', true); echo $res; ?>

Code execution result:

'ab'

Example

Search with UTF-8 encoding specified:

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

Code execution result:

'мир'

See Also

  • the mb_strpos function,
    which searches for the position of the first occurrence of a substring
  • the strstr function,
    which performs a similar search without considering multibyte encodings
English
AfrikaansAzərbaycanБългарскиবাংলাБеларускаяČeštinaDanskDeutschΕλληνικάEspañolEestiSuomiFrançaisहिन्दीMagyarՀայերենIndonesiaItaliano日本語ქართულიҚазақ한국어КыргызчаLietuviųLatviešuМакедонскиMelayuမြန်မာNederlandsNorskPolskiPortuguêsRomânăРусскийසිංහලSlovenčinaSlovenščinaShqipСрпскиSrpskiSvenskaKiswahiliТоҷикӣไทยTürkmenTürkçeЎзбекOʻzbekTiếng Việt
We use cookies for website operation, analytics, and personalization. Data processing is carried out in accordance with the Privacy Policy.
accept all customize decline