Monday, February 23, 2026
HomeLanguagesPHP mb_substr() Function

PHP mb_substr() Function

The mb_substr() is an inbuilt function in PHP that is used to extract a portion of a string, based on a specified starting position and length, while taking into account the multi-byte character encoding of the string.

Syntax:

mb_substr( string $string, int $start, ?int $length = null, ?string $encoding = null )
: string

Parameters: This function takes four parameters that are described below.

  • $string: The multi-byte string from which to extract the substring.
  • $start: The position in the string to start the extraction. If negative, the position is relative to the end of the string.
  • $length: The length of the substring to extract. If not provided, the function returns the portion of the string starting from the $start position until the end of the string.
  • $encoding: The character encoding of the string. If not provided, internal encoding is used.

Return Value: This function returns the specific portion of the string specified by the start and length parameters.

Example 1: The following program demonstrates the mb_substr() function.
 

PHP




<?php
$str = "Hello, world!";
$sub = mb_substr($str, -6);
echo $sub;
?>


Output:                                                                                                                                              

world!

Example 2: The following program demonstrates the mb_substr() function.
 

PHP




<?php
$str = "Geeks for Geeks";
$sub = mb_substr($str, 3,-6);
echo $sub;
?>


Output:

ks for  

Reference: https://www.php.net/manual/en/function.mb-substr.php

RELATED ARTICLES

Most Popular

Dominic
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS