Collecting sub string from any part of a string
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. Here is the syntax of substr function.
string substr (string string, int start [, int length])
We can see the optional element of length which will collect that many length of string.
Removing part of the string from starting
Output of above code is efghijklmno
Copying part of the string from starting
For example from a string of characters we want to copy a part of the string then we can use the substr function. Let us take a string having values of “abcdefghijklmno”.
This will print abcd to the string. Here we have set the staring element as 0 so from the beginning 4 characters are collected.
Removing Part of the string after x chars
This will print cdef. Here as we have used 2 , so 4 characters starting from 2 character will be taken.
Part of the string from the end
By using negative number in place of starting location we can pick up sub strings from the end of the string. Like this
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.
Be the first to post comment on this article :