PHP: Split string into array, like explode with no delimiter

121,550

Solution 1

$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"

Solution 2

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

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

prints 'a'

Solution 3

Try this:

$str = '123456789';
$char_array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);

Solution 4

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.

Solution 5

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.

Share:
121,550
oni-kun
Author by

oni-kun

Updated on July 05, 2022

Comments

  • oni-kun
    oni-kun almost 2 years

    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.

  • Erik
    Erik over 14 years
    That's really unnecessary, and quite a bit slower then str_split.
  • oni-kun
    oni-kun over 14 years
    Ah. Missed that function, was splitting binary numbers to becalculated in an array, this works well.
  • hakre
    hakre almost 11 years
    @Erik: Not if you need to get back an empty array in case the $str has no length.
  • Travis Weston
    Travis Weston over 9 years
    @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.
  • kicaj
    kicaj almost 8 years
    What about encoding?
  • bdsl
    bdsl almost 7 years
    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
    bdsl almost 7 years
    @kicaj In the comments at php.net/mb_split you can see a function written by adjwilli which is supposed to split a UTF8 string into characters.
  • mickmackusa
    mickmackusa about 2 years
    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
    mickmackusa about 2 years
    This answer is missing its educational explanation.
  • mickmackusa
    mickmackusa about 2 years
    Please define what "special characters" means. I know what you mean, but other researchers will not. "THAN" should be "then".
  • mickmackusa
    mickmackusa about 2 years
    Why try explode('', $string); if the OP already said that it makes PHP barf?