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. We can see the optional element of length which will collect that many length of string.
Removing part of the string from a position till end
Copying part of the string from starting till a position
This will print abcd to the string. Here we have set the staring element as 0 so from the beginning 4 characters are collected.
Part of the string after x chars ( positive $start , positive $length)
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, negative $startBy using negative number in place of starting location we can pick up sub strings from the end of the string. Like this
Negative $start, positive $length
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.
Negative $start, negative $length
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()
Subscribe to our YouTube Channel here
This article is written by plus2net.com team. https://www.plus2net.com