Page 1 of 1
variation of string::explode() that works?
Posted: Tue Mar 09, 2010 4:48 am
by joppe
$arr = explode("/","//a");
why do i get 2 empty cells before "a" in my array?
$arr[0] == ""
$arr[1] == ""
$arr[2] == "a"
count($arr) == 3
what i'd expect is
$arr[0] == "a"
count($arr) == 0
not sure about the php version, phpinfo is blocked and i have no control over the server - i do know it's oldish
Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 5:24 am
by s.dot
do array_filter(explode('/', '//a'));
Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 5:31 am
by joppe
that does half the job
i've got an array with count() of 1 like expected, but the value "a" is in cell [2]
i could grab the count()-1 from before the array_filter and use that to point to the value afterwards, but that seems a bit contrived...
Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 5:36 am
by s.dot
This is starting to feel ugly, lol... but..
array_values(array_filter(explode('/', '//a')));
Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 5:41 am
by joppe
thanks that seems to work... ugly tho' it may be

Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 9:36 am
by Weirdan
Code: Select all
$arr = preg_split("#/#","//a", -1, PREG_SPLIT_NO_EMPTY);
Re: variation of string::explode() that works?
Posted: Tue Mar 09, 2010 10:21 am
by AbraCadaver
Your example is simplistic so I don't know what other inputs you'll have, but strtok() might be of use.