PHP String substr() Function

The substr() is a built-in function of PHP, which is used to extract a part of a string. The substr() function returns a part of a string specified by the start and length parameter. PHP 4 and above versions support this function.

Syntax

The syntax of the substr() function is given below. It contains three parameters, in which two are mandatory, and one is optional.

Parameters

$string (required) - It is the main string parameter, which specifies the string that needs to be cut or modified. This is a mandatory parameter of this function. It specifies the input string that must be one character or longer.

$start (required) - This is also a mandatory parameter of this function as $string. It specifies that from where to start extraction in the string. This parameter contains an integer value, which has three conditions:

  • If $start has a positive value, then the returned string will start from $startth position in the string, and counting begins from zero.
  • If $start has a negative value, then the returned string will start from $startth character from the end of the string. In this case, counting begins from -1 not from zero at the end of the string.
  • If the $string size is less than $start value, then it will return FALSE.

$length (optional) - This is an integer type and optional parameter of this function. This parameter has the length of the string to be cut from the main string. It has the following conditions:

  • If $length is positive, then the returned string will contain the number of character passed in $length parameter that will begin from the $start.
  • If $length is negative, then it begins from $start position and extracts the length from the end of the string. Many of the characters will be omitted from the end of the string if the value passed in $length parameter is negative.
  • If the value passed in $length parameter is zero, FALSE or NULL, then an empty string will be returned (See in example 3).
  • If the $length parameter is not passed, then substr() function will return a string starting from $start till the end of the string.

Return Values

The substr() function returns an extracted part of the string on successful execution. Otherwise, it returns the FALSE or empty string on failure.

Changelog

  • In PHP 7.0.0, if $string is equal to the $start, then substr() function returns an empty string. Before this version, FALSE was returned in this case.
  • In PHP 5.2.2 - PHP 5.2.6, if the $start parameter indicates the position of negative truncation or beyond, then FALSE will be returned, whereas other versions get the string from $start.

Examples

There are some examples given, through which we can learn the working of the substr() function. Let's see the below examples-

Example 1

Output:

The output for the above program is given below.

lo javaTpoint
Hello javaTpoint
aTpoint
oint
JavaTpoint
Hello javaTpoint

Example 2

Output:

The output for the above program is given below.

lo javaT
Hello jav
javaTp
o jav

Tpoin

Example 3

Output:

When a zero or Null value is passed in $length, then the substr() function returns an empty string.

No output





Latest Courses