PHP Explode Each Character Function

This function is like PHP's explode() funtion, but instead of having a delimiter, it separates each character into it's own array piece.

For example, explodeEachChar('cat') would return array(0 => 'c', 1 => 'a', 2 => 't').


function explodeEachChar($x) {
    $c = array();
    while (strlen($x) > 0) {
        $c[] = substr($x,0,1);
        $x = substr($x,1);
    return $c;


Note: Of course, if you're on PHP 5 or greater, this can be done more simply using PHP's built-in str_split() function.



This post was published on November 8th, 2009 by Robert James Reese in PHP Before using any of the code or other content in this post, you must read and agree to our terms of use.