PHP String substr() FunctionThe 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. SyntaxThe 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:
$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:
Return ValuesThe substr() function returns an extracted part of the string on successful execution. Otherwise, it returns the FALSE or empty string on failure. Changelog
ExamplesThere 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 Next TopicPHP String Functions |