PHP substr(): How to Extract a Substring from a String (original) (raw)

Summary: in this tutorial, you’ll learn how to use the PHP substr() function to extract a substring from a string.

Introduction to the PHP substr() function #

The substr() function accepts a string and returns a substring from the string.

Here’s the syntax of the substr() function:

substr ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo separator="true">,</mo><mi>i</mi><mi>n</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">string , int </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span></span></span></span>offset , int|null $length = null ) : stringCode language: PHP (php)

In this syntax:

PHP substr() function examples #

Let’s take some examples of using the substr() function.

1) Simple PHP substr() function example #

The following example uses the substr() function to extract the first three characters from a string:

`<?php

$s = 'PHP substring'; result=substr(result = substr(result=substr(s, 0, 3);

echo $result;// PHP`Code language: PHP (php)

Try it

In this example, the substr() function extract the first 3 characters from the 'PHP substring' string starting at the index 0.

PHP substr() function

2) Using PHP substr() function with the default length argument #

The following example uses the substr() function to extract a substring from the 'PHP substring' string starting from the index 4 to the end of the string:

`<?php

$s = 'PHP substring'; result=substr(result = substr(result=substr(s, 4);

echo $result; // substring`Code language: PHP (php)

Try it

In this example, we omit the $length argument. Therefore, the substr() returns a substring, starting at index 4 to the end of the input string.

PHP substr() function with default length

PHP substr() function with negative offset #

The $offset argument can be a negative number. If the $offset is negative, the substr() function returns a substring that starts at the offset character from the end of the string. The last character in the input string has an index of -1.

The following example illustrates how to use the substr() function with negative offset:

`<?php

$s = 'PHP is cool'; result=substr(result = substr(result=substr(s, -4);

echo $result; // cool`Code language: PHP (php)

Try it

In this example, the substr() returns a substring that at 4th character from the end of the string.

The following picture illustrates how the substr() function works in the above example:

PHP substr() function with negative offset

PHP substr() function with negative length #

Like the $offset argument, the $length argument can be negative. If you pass a negative number to the $length argument, the substr() function will omit a $length number of characters in the returned substring.

The following example illustrates how to use the substr() with a negative $offset and $length arguments:

`<?php

$s = 'PHP is cool'; result=substr(result = substr(result=substr(s, -7, -5);

echo $result; // is`Code language: PHP (php)

Try it

The following picture illustrates how the above example works:

PHP substr() function with negative length

The PHP mb_substr() function #

See the following example:

`<?php

$message = 'adiós'; result=substr(result = substr(result=substr(message, 3, 1);

echo $result;`Code language: PHP (php)

This example attempts to extract a substring with one character in the $message string starting at index 3. However, it shows nothing in the output.

The reason is that the $message string contains a non-ASCII character. Therefore, the substr() function doesn’t work correctly.

To extract a substring from a string that contains a non-ASCII character, you use the mb_substr() function. The mb_substr() function is like the substr() function except that it has an additional encoding argument:

mb_substr ( string <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mo separator="true">,</mo><mi>i</mi><mi>n</mi><mi>t</mi></mrow><annotation encoding="application/x-tex">string , int </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.854em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span></span></span></span>start , int|null <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>l</mi><mi>e</mi><mi>n</mi><mi>g</mi><mi>t</mi><mi>h</mi><mo>=</mo><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi><mo separator="true">,</mo><mi>s</mi><mi>t</mi><mi>r</mi><mi>i</mi><mi>n</mi><mi>g</mi><mi mathvariant="normal">∣</mi><mi>n</mi><mi>u</mi><mi>l</mi><mi>l</mi></mrow><annotation encoding="application/x-tex">length = null , string|null </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">n</span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal">s</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">in</span><span class="mord mathnormal" style="margin-right:0.03588em;">g</span><span class="mord">∣</span><span class="mord mathnormal">n</span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.01968em;">ll</span></span></span></span>encoding = null ) : stringCode language: PHP (php)

The following example uses the mb_substr() function to extract a substring from a string with non-ASCII code:

`<?php

$message = 'adiós'; result=mbsubstr(result = mb_substr(result=mbsubstr(message, 3, 1);

echo $result;`Code language: PHP (php)

Output:

óCode language: PHP (php)

PHP substr helper function #

The following defines a helper function that uses the mb_substr() function to extract a substring from a string:

`<?php

function substring($string, start,start, start,length = null) { return mb_substr($string, start,start, start,length, 'UTF-8'); }`Code language: PHP (php)

Summary #

Did you find this tutorial useful?