98 of 410 menu

The mb_strrchr Function

The mb_strrchr function searches for the last occurrence of a character in a string and returns the part of the string from that character to the end. Unlike strrchr, it works with multibyte encodings (UTF-8, etc.). The first parameter is the string to search in, the second is the character to search for, the third is an optional parameter for searching before the character, and the fourth is the encoding.

Syntax

mb_strrchr( string $haystack, string $needle, bool $before_needle = false, string $encoding = null );

Example

Find the last occurrence of a character in a string:

<?php $res = mb_strrchr('abcba', 'b'); echo $res; ?>

Code execution result:

'ba'

Example

Find the part of the string before the last occurrence of the character 'b':

<?php $res = mb_strrchr('abcba', 'b', true); echo $res; ?>

Code execution result:

'abc'

Example

Working with Cyrillic in UTF-8:

<?php $res = mb_strrchr('привет мир', 'и'); echo $res; ?>

Code execution result:

'ир'

See Also

  • the mb_strpos function,
    which searches for the position of the first occurrence of a substring
  • the mb_substr function,
    which returns a part of a string
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