PHP substr() Function

What is PHP substr() function?

If you want to extract (get) part of a string (substring) from a string according to your requirement, use substr() function. For extraction, you’ll mention two things in the string– one, the starting point, and two, the number of characters to extract or the number of characters to omit from the end and get the remaining substring. It is one of the most used PHP built-in functions in PHP.

Syntax:

substr(string, starting_position, length_to_extract_or_omit)

Parameters:

The function has 2 required parameters and 1 optional parameter-

string (required): String to be operated on.

starting_position (required): Refers to the position to start cutting in the string. It could be a positive or negative number-

  • For positive starting_position: It indicates the starting_positionth character as the first character to start cutting (Check row 1 of Table 1). Note, the position of the first character in the string is considered as zero.
  • For negative starting_position: It indicates the starting_positionth character starting from the end of the string as the first character to start cutting (Check row 2 of Table 1).
  • For zero starting_position: It refers to the starting_positionth character as the first character to startcutting (Check row 3 of Table 1).
  • If starting_position is greater in number than the string length: The function returns empty string (Check row 4 of Table 1).
  • Table 1:
    StringExampleResult
    1substr functionsubstr(“substr function”, 2)bstr function
    2substr functionsubstr(“substr function”, -2)on
    3substr functionsubstr(“substr function”, 0)substr function
    4substr functionsubstr(“substr function”, 50)

length_to_extract_or_omit (optional): It could be a positive or negative number-

  • For positive length_to_extract_or_omit: The function extracts and returns length_to_extract_or_omit characters starting from the starting_position of the string (Check row 1 of Table 2). If there are not enough characters to be extracted, the function returns as many characters as exist (Check row 1 of Table 2).
  • For negative length_to_extract_or_omit: The function omits length_to_extract_or_omit characters from the end of the string and returns the remaining characters (Check row 2 of Table 2).
  • For zero length_to_extract_or_omit: The function returns an empty string (Check row 3 of Table 2).
  • For NULL or omitted length_to_extract_or_omit: The function returns substring starting from starting_position to the end (Check row 4 of Table 2).
  • Table 2:
    StringExampleResult
    1substr functionsubstr(“substr function”, 3, 1)s
    2substr functionsubstr(“substr function”, 7, 10)function
    3substr functionsubstr(“substr function”, 3, 0)
    4substr functionsubstr(“substr function”, 3, NULL)str function

Return Values:

The function returns the extracted part of the string or an empty string.

Examples:

Example 1:

<?php
echo substr("substr function", 2) . "<br />"; // bstr function
echo substr("substr function", -2) . "<br />"; // on
echo substr("substr function", 0) . "<br />"; // substr function
echo substr("substr function", 50) . "<br />"; // 
?>

Output:

bstr function
on
substr function

Example 2:

<?php
echo substr("substr function", 3, 1) . "<br />"; // s
echo substr("substr function", 7, 10) . "<br />"; // function
echo substr("substr function", 3, 0) . "<br />"; // 
echo substr("substr function", 3, NULL) . "<br />"; // str function
?>

Output:

s
function

str function

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP substr() Function

One of the most frequently used built-in PHP string functions.

Reference:

https://www.php.net/manual/en/function.substr.php