How to Find Part of String - substr() Function

PHP String Functions

The substr() function returns a part of a string specified by the offset and length parameters.

images/articles/php/find-part-of-string-substr.jpg

Syntax

substr($string, $start, $length)

Parameters

  1. string (Required) - Specifies the string to return a part of.
  2. 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.
  3. length (Optional) - Specifies the length of the returned string. Default is to the end of the string.

Example

$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"