79

I have a string such as:

"0123456789"

And I 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
oni-kun
  • 1,775
  • 6
  • 17
  • 22

9 Answers9

139
$array = str_split("0123456789bcdfghjkmnpqrstvwxyz");

str_split takes an optional 2nd param, the chunk length (default 1), 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"
David Harkness
  • 35,992
  • 10
  • 112
  • 134
Erik
  • 20,526
  • 8
  • 45
  • 76
  • Ah. Missed that function, was splitting binary numbers to becalculated in an array, this works well. – oni-kun Jan 31 '10 at 02:42
  • 1
    What about encoding? – kicaj Jul 19 '16 at 12:47
  • PHP doesn't understand encoding. It will just split the string into bytes, so any multi-byte characters will get messed up. PHP6 was supposed to fix that but it didn't happen. – bdsl Jul 16 '17 at 16:03
  • @kicaj In the comments at http://php.net/mb_split you can see a function written by adjwilli which is supposed to split a UTF8 string into characters. – bdsl Jul 16 '17 at 16:04
19

You can access characters in a string just like an array:

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

prints 'a'

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nategood
  • 11,807
  • 4
  • 36
  • 44
  • Of course, this answer makes no attempt to split a string, it just demonstrates how you can access byte values in a string by their offset. – mickmackusa Feb 06 '22 at 11:59
7

Try this:

$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
Quill
  • 2,729
  • 1
  • 33
  • 44
Conan
  • 2,659
  • 17
  • 24
  • 1
    That's really unnecessary, and quite a bit slower then str_split. – Erik Jan 31 '10 at 02:37
  • @Erik: Not if you need to get back an empty array in case the `$str` has no length. – hakre Jun 26 '13 at 13:23
  • @hakre In that instance, it would be ample amounts faster to simply do a strlen check on `$str` and set `$char_array = array()` if `strlen` returns 0. – Travis Weston Aug 23 '14 at 17:24
5

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
4

Here is an example that works with multibyte (UTF-8) strings.

$str = 'äbcd';

// PHP 5.4.8 allows null as the third argument of mb_strpos() function
do {
    $arr[] = mb_substr( $str, 0, 1, 'utf-8' );
} while ( $str = mb_substr( $str, 1, mb_strlen( $str ), 'utf-8' ) );

It can be also done with preg_split() (preg_split( '//u', $str, null, PREG_SPLIT_NO_EMPTY )), but unlike the above example, that runs almost as fast regardless of the size of the string, preg_split() is fast with small strings, but a lot slower with large ones.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Danijel
  • 12,408
  • 5
  • 38
  • 54
1

Try this:

$str = "Hello Friend";

$arr1 = str_split($str);
$arr2 = str_split($str, 3);

print_r($arr1);
print_r($arr2);

The above example will output:

Array
(
    [0] => H
    [1] => e
    [2] => l
    [3] => l
    [4] => o
    [5] =>
    [6] => F
    [7] => r
    [8] => i
    [9] => e
    [10] => n
    [11] => d
)

Array
(
    [0] => Hel
    [1] => lo
    [2] => Fri
    [3] => end
)
Quill
  • 2,729
  • 1
  • 33
  • 44
1

Try this:

    $str = '546788';
    $char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
Quill
  • 2,729
  • 1
  • 33
  • 44
Rekha
  • 41
  • 5
0

If you want to split the string, it's best to use:

$array = str_split($string);

When you have a delimiter, which separates the string, you can try,

explode('', $string);

Where you can pass the delimiter in the first variable inside the explode such as:

explode(',', $string);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Manoj Rammoorthy
  • 1,384
  • 16
  • 16
0
$array = str_split("$string");

will actually work pretty fine, but if you want to preserve the special characters in that string, and you want to do some manipulation with them, then I would use

do {
    $array[] = mb_substr($string, 0, 1, 'utf-8');
} while ($string = mb_substr($string, 1, mb_strlen($string), 'utf-8'));

because for some of mine personal uses, it has been shown to be more reliable when there is an issue with special characters.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Nedzad Ganic
  • 25
  • 1
  • 9