79 of 410 menu

The mb_substr Function

The mb_substr function extracts and returns a substring from a string, correctly working with multibyte encodings (e.g., UTF-8). The first parameter is the string, the second is the starting position, the third (optional) is the substring length. The fourth parameter can specify the encoding.

Syntax

mb_substr(string, start, [length], [encoding]): string;

Example

Let's extract 3 characters from the string, starting from position 1:

<?php $res = mb_substr('абвгд', 1, 3); echo $res; ?>

Code execution result:

'бвг'

Example

Let's extract a substring to the end of the string from "日本語", starting from position 1:

<?php $res = mb_substr('日本語', 1); echo $res; ?>

Code execution result:

'本語'

Example

Let's extract a substring specifying the UTF-8 encoding:

<?php $res = mb_substr('абвгд', 2, 2, 'UTF-8'); echo $res; ?>

Code execution result:

'вг'

See Also

  • the mb_strlen function,
    which returns the length of a string in a multibyte encoding
  • the substr function,
    which works similarly but without multibyte encoding support
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