Backend Development 6 min read

Using substr() and mb_substr() to Extract Substrings in PHP

This article explains how PHP's substr() and mb_substr() functions work, detailing their syntax, parameter behavior for positive and negative start and length values, and provides multiple code examples demonstrating string extraction for both English and multibyte Chinese characters.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using substr() and mb_substr() to Extract Substrings in PHP

1. Using substr() to extract substrings

The substr() function returns a portion of a string starting at a specified position with an optional length. Its syntax is substr($string, $start [, $length]) . Parameters:

$string : the input string, must contain at least one character.

$start : the start index; if non‑negative, counting begins at 0 from the left, if negative, counting starts from the end of the string.

$length (optional): the number of characters to extract. Positive values extract forward, negative values omit characters from the end, zero, FALSE or NULL return an empty string, and omission means extraction continues to the end of the string.

Example usage with various positive and negative start and length values:

<code>&lt;?php
echo substr("Hello world",0,10)."<br>";
echo substr("Hello world",1,8)."<br>";
echo substr("Hello world",0,5)."<br>";
echo substr("Hello world",6,6)."<br>";
echo substr("Hello world",0,-1)."<br>";
echo substr("Hello world",-10,-2)."<br>";
echo substr("Hello world",0,-6)."<br>";
echo substr("Hello world",-2,-3)."<br>";
?&gt;</code>

Output:

<code>Hello worl
ello wor
Hello
world
Hello worl
ello wor
Hello
world</code>

2. Using mb_substr() to extract multibyte (e.g., Chinese) strings

The mb_substr() function works like substr() but supports multibyte encodings, allowing correct extraction of Chinese characters. Syntax: mb_substr($str, $start [, $length = NULL [, $encoding = mb_internal_encoding()]]) . Parameters:

$str : the input string containing at least one character.

$start : start position; non‑negative counts from the left, negative counts from the right.

$length (optional): length of the substring; positive extracts forward, negative omits characters from the end, NULL or omitted extracts to the end.

$encoding (optional): character encoding; defaults to the internal encoding if omitted.

Example demonstrating extraction of Chinese text:

<code>&lt;?php
    $str = '欢迎访问PHP中文网,一个在线学习编程的网站。';
    echo mb_substr($str, 4)."<br>";
    echo mb_substr($str, -19)."<br>";
    echo mb_substr($str, 4, 6)."<br>";
    echo mb_substr($str, 4, -16)."<br>";
    echo mb_substr($str, -19, -13)."<br>";
    echo mb_substr($str, -19, 6)."<br>";
    var_dump(mb_substr($str, 40));
    echo "<br>";
    var_dump(mb_substr($str, 4, null));
?&gt;</code>

Resulting output:

<code>PHP中文网,一个在线学习编程的网站。
PHP中文网,一个在线学习编程的网站。
PHP中文网
PHP
PHP中文网
PHP中文网
string(0) ""
string(55) "PHP中文网,一个在线学习编程的网站。"</code>
backendPHPstring-manipulationmb_substrsubstr
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.