I have several strings that look like:
longname1, anotherlongname2, has1numbers2init3
I would like to use str_split
to split off the last character of the strings. Eg:
Array ( [0] => longname [1] => 1 ) Array ( [0] => anotherlongname [1] => 2 ) Array ( [0] => has1numbers2init [1] => 3 )
I can get the number alone using substr($string, -1);
but need to find an efficient way of retrieving the remainder of the string.
I have tried:
str_split($string,-1);
but of course this doesn’t work.
Would anyone know what I could do?
Advertisement
Answer
You can subtract one from the strlen()
of your $string
inside of str_split()
:
<?php $string = 'longname1'; print_r(str_split($string, strlen($string) - 1));
Output:
Array ( [0] => longname [1] => 1 )
This can be seen working here.