Code

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').

<?php

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.

Last updated on August 13, 2010.

Copyright © 2010, Ink Plant. All rights reserved.