Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 in another string variable. How may I do this with substring? With PHP

$rest = substr("jKsuSportTopics", -3, 1);
$rest2 = substr("jKsuSportTopics", 4, 0);
like image 976
GivenPie Avatar asked Feb 10 '13 02:02

GivenPie


1 Answers

The second argument is the starting index, and the third argument is the length of the string. If the 3rd argument is missing, you'll 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 the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.

[...]

If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

[...]

If length is omitted, the substring starting from start until the end of the string will be returned.

like image 127
Joseph Silber Avatar answered Oct 02 '22 23:10

Joseph Silber