substr (string $string, int $start [, int $length])
We can take out a part of a string by using substr function in PHP. This way starting from any location to any location we can copy the string.
We can see the optional element of length which will collect that many length of string.
Required : Input string
Required : if not negative then return sting will start from $start position.
if negative then return string will start from $start characters from end of the string.
if start char is more than the length of the input string then FALSE is returend.
The first char is 0 position from left.
Optional : if Positive then starting from $start, $length number of chars are returned.
if negative then $length number of chars will be omitted from end of the string.
if $length is 0 or False or null then an empty string is returned.
if $length is omitted then the string starting from $start till the end is returned.
Removing part of the string from a position till end
echo $string; // Output is efghijklmno
Copying part of the string from starting till a position
The above line of code will return jklm. You can see the negative number 6 mean the function will start collecting from the 6th position from the end. That is the from the character j . Now from j 4 characters, mean it is jklm.
The above line of codes will return jklmn. You can see the negative number 6 mean the function will start collecting from the 6th position from the end. That is the from the character j . Now from j all chars except last char, mean it is jklmn.
Using substr() we can collect part of the string of any length from any position. However to collect part of the string we need to know the position of the searched string. To know the position of any searched string from starting or ending we have to use stripos()