How To Find Part of String - substr() Function

PHP String Functions

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


substr(string, start, length)


  • 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.


$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