The substr() function returns a part of a string.

Syntax

substr(string, start, length)

Parameters

  • string (Required) - Specifies the string to return a part of
  • start (Required) - Specifies where to start in the string. A positive number starts at a specified position in the string. A negative number starts at a specified position from the end of the string. 0 means to start at the first character in string
  • length (Optional) - Specifies the length of the returned string. Default is to the end of the string.

Example

<?php
$rest = substr("abcdef", 0, -1); // returns "abcde"
$rest = substr("abcdef", 2, -1); // returns "cde"
$rest = substr("abcdef", 4, -4); // returns false
$rest = substr("abcdef", -3, -1); // returns "de"
?>

In PHP version, 7.0, if string is equal to start characters long, an empty string will be returned. Prior to this version, FALSE was returned in this case.

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive