Friday, October 24, 2025
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS