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