The substr() function is used to return a part of a string.
Syntax
substr(str, begin, len)
Parameters
str − The string
begin − Specifies where to begin in the string
A positive number − Start at a specified position in the string
A negative number − Start at a specified position from the end of the string
0 − Start at the first character in string
len − Specifies the length of the returned string. Default is to the end of the string.
A positive number − The length to be returned from the start parameter
Negative number − The length to be returned from the end of the string
Return
The substr() function returns the extracted part of the string.
Example
The following is an example −
<?php
echo substr("This is it!",5);
?>Output
is it!
Example
The following is an example −
<?php
echo substr("programming language",2,6)."<br>"
?>Output
ogramm <br>
Example
Let us see another example that includes negative parameters −
<?php
echo substr("website development",-5,-2)."<br>"
?>Output
pme <br>