I have a string such as:

"0123456789"

and need to split EACH character into an array.

I for the hell of it tried:

explode('', '123545789');

But it gave me the obvious: Warning: No delimiter defined in explode) ..

How would I come across this? I can't see any method off hand, especially just a function

share|improve this question

feedback

4 Answers

up vote 24 down vote accepted
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split also takes a 2nd param, the chunk length, so you can do things like:

$array = str_split("aabbccdd",2);

// $array[0] = aa
// $array[1] = bb
// $array[2] = cc  etc ...

You can also get at parts of your string by treating it as an array:

$string = "hello";
echo $string[1];

// outputs "e"
share|improve this answer
Ah. Missed that function, was splitting binary numbers to becalculated in an array, this works well. – oni-kun Jan 31 '10 at 2:42
feedback

Try this:

<?php
$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
?>
share|improve this answer
That's really unnecessary, and quite a bit slower then str_split. – Erik Jan 31 '10 at 2:37
feedback

str_split can do the trick. Note that strings in PHP can be accessed just like a chars array, in most cases, you won't need to split your string into a "new" array.

share|improve this answer
feedback

What are you trying to accomplish? You can access characters in a string just like an array

$s = 'abcd';
echo $s[0];

prints 'a'

share|improve this answer
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.