Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php explode all characters [duplicate]

I'm looking for the equivalent of what in js would be 'this is a string'.split('') for PHP.

If I try $array = explode('', 'testing'); I get an error Warning: explode() [function.explode]: Empty delimiter in

Is there another way to do this?

like image 931
qwertymk Avatar asked Mar 21 '12 23:03

qwertymk


3 Answers

As indicated by your error, explode requires a delimiter to split the string. Use str_split instead:

$arr = str_split('testing');

Output

Array
(
    [0] => t
    [1] => e
    [2] => s
    [3] => t
    [4] => i
    [5] => n
    [6] => g
)
like image 117
Josh Avatar answered Nov 15 '22 11:11

Josh


Use the str_split function.

$array = str_split( 'testing');
like image 8
nickb Avatar answered Nov 15 '22 10:11

nickb


$string = "TEST";

echo $string[0];  // This will display T

There is no need to explode it

like image 6
Mark Roach Avatar answered Nov 15 '22 10:11

Mark Roach