PHP Substring Function With Examples

In PHP, substring or substr() is a function which returns some part of a given string.

In this article, we will talk about PHP substring or substr() in a detailed explanation as well as example programs that could be helpful to your learning on this PHP function. This topic is a continuation of the previous topic, entitled PHP Switch Case.

What is PHP Substring?

The PHP substring or substr() is a function which is already built-in PHP that can be used to bring out part of the given string. The function is mainly used to return the specified substring from the start and parameter length.

Further, the function is supported by PHP 4 version and above such as (5, 6, 7, and 8).

Syntax

substr(string,start,length)

Parameter Values

ParameterValues
stringThis parameter is (REQUIRED) – State the string that needs to return into a part of a given string.
startThis parameter is (REQUIRED) – State on where to start the given string.
— Once the given integer is positive, It means that the return string will start from that position in the given string.
— Once the given integer is negative, It means that the return string will start from the end of that position in the given string.
— Once the given integer is 0, It means that the return string will start from the first character in the given string.
lengthThis parameter is an (OPTIONAL), and this is an integer with a referring value to the length of the given string which has been cut into the original one.
— Once the given length is positive, It means that the substring includes a number of characters passed on the parameter from the beginning of the given length.
— Once the given length is negative, It means that the substring begins from the starting position and bring out the length from the end of a string.
— Once the parameter is not passed, It means that the function will return a string from the starting position till the end of the given character.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <?php
    echo substr("substr in php",3);
    ?>
</body>
</html>

Output

str in php

More PHP substring examples

substr() basic usage

<?php
echo substr('Glenn', 1)."\n";     // lenn
echo substr("Jude", 1, null)."\n"; // ude; prior to PHP 8.0.0, returned an empty string was returned false
echo substr('Adones', 1, 3)."\n";  // don
echo substr('Paul', 0, 4)."\n";  // Paul
echo substr('Caren', 0, 8)."\n";  // Caren
echo substr('Elijah', -1, 1)."\n"; // h

// Accessing single characters in a string
// can also be achieved using "square brackets"
$string = 'Glenn';
echo $string[0]."\n";                 // G
echo $string[3]."\n";                 // n
echo $string[strlen($string)-1]; // n
?>

Output

lenn

don
Paul
Caren
h
G
n
n

substr() with the use of a negative offset

<?php
$name1 = substr("Glenn Magada Azuelo", -3);    // returns "elo"
$name2 = substr("Angel Jude Suarez", -5);    // returns "uarez"
$name3 = substr("Adones Evangelista", -2, 1); // returns "t"

echo $name1."\n";
echo $name2."\n";
echo $name3;
?>

Output

elo
uarez
t

substr() with the use of negative length

<?php
$name1 = substr("Glenn Magada Azuelo", 0, -2);  // returns "Glenn Magada Azue"
$name2 = substr("Angel Jude Suarez", 3, -4);  // returns "el Jude Su"
$name3 = substr("Adones Evangelista", 4, -4);  // returns "es Evangel"
$name4 = substr("John Paul Blauro", -4, -1); // returns "aur"

echo $name1."\n";
echo $name2."\n";
echo $name3."\n";
echo $name4;
?>

Output

Glenn Magada Azue
el Jude Su
es Evangel
aur

substr() with casting behaviour

<?php
class car {
    public function __toString() {
        return "blue";
    }
}

echo "1) ".var_export(substr("bmw", 0, 2), true).PHP_EOL;
echo "2) ".var_export(substr(54321, 0, 2), true).PHP_EOL;
echo "3) ".var_export(substr(new car(), 0, 2), true).PHP_EOL;
echo "4) ".var_export(substr(true, 0, 1), true).PHP_EOL;
echo "5) ".var_export(substr(false, 0, 1), true).PHP_EOL;
echo "6) ".var_export(substr("", 0, 1), true).PHP_EOL;
echo "7) ".var_export(substr(1.2e3, 0, 4), true).PHP_EOL;
?>

Output

1) 'bm'
2) '54'
3) 'bl'
4) '1'
5) ''
6) ''
7) '1200'

How can I get the first 5 characters of a string in PHP?

The following code below is a way to get the substring of the first 5 characters of a given string.

Example

<?php
         // singlebyte strings
        $result = substr("Glenn Magada Azuelo", 0, 5);
        echo $result;
?>

Output

Glenn

How do I extract part of a string?

The substr() function is used to extract part of a length of the string which begins in the specified position in the string and returns a number of characters which has been specified.

Further, the substr() function could not change the original string. In some instances where you want to extract characters from the end of the given string you just need to use a negative start position.

What is the difference between substr and Instr?

The difference between substr and Instr is their functionalities, the substr is a functions which allows you to extract a PHP substring from a given string. While Instr is a function which returns a location of a substring in a given input string.

What does Substr function return?

The substr() is a PHP function that returns a character from the given string value starting from the character position which has been specified by the start. Further, the number of characters that will be returned is specified by the length.

Summary

In summary, you have learned about PHP Substring. This article also discussed what does substr() do, how to get the first 5 characters of a string, how to extract part of a string, what is the difference between substr and Instr, and what does Substr function return.

I hope this lesson has helped you learn a lot. Check out my previous and latest articles for more life-changing tutorials which could help you a lot.

Leave a Comment