Substring to take all letters after the 4th character
I only have a basic understanding of substring. I am trying to extract the first 4 characters as a string variable and extract the rest of the characters into another string variable. How can I do this using a substring? With PHP
$rest = substr("jKsuSportTopics", -3, 1);
$rest2 = substr("jKsuSportTopics", 4, 0);
source to share
The second argument is the starting index and the third argument is the length of the string. If the third argument is missing, you get the rest of the string.
$first_part = substr("jKsuSportTopics", 0, 4);
$rest = substr("jKsuSportTopics", 4);
Here's a quote from the docs :
... the returned string will start at
start
'th position instring
, counting from zero. For example, in the string "abcdef" the character at position 0 is "a", the character at position 2 is "c", and so on.[...]
If
length
given and positive, the returned string will contain no more thanlength
characters starting withstart
(depending on the length of the string).[...]
If
length
omitted, substring starting withstart
, until the end of the string is returned.
source to share