Page 1 of 1

explode help

Posted: Sat Apr 22, 2006 3:34 pm
by elecktricity
how would I explode a variable by characters like:

Code: Select all

<?PHP
$variable = 'hello all';
/* explode
$var = explode('', $variable);
*/
$var[0] = 'h';
$var[1] = 'e';
$var[2] = 'l';
$var[3] = 'l';
$var[4] = 'o';
$var[5] = ' ';
$var[6] = 'a';
$var[7] = 'l';
$var[8] = 'l';
?>

Posted: Sat Apr 22, 2006 3:41 pm
by ambivalent
str_split() seems to be what you need.

Posted: Sat Apr 22, 2006 3:45 pm
by themurph
You wouldnt want to use explode to do that.. explode is usually used to parse values stored with a specific character as a delimiter like "\t" ot "," or "|"

Mabye try something like this instead:

Code: Select all

$variable = 'hello all'; 
$var= array();
for ($x=0; $x < $strlen($variable); $x++) { $var[$x] = $variable[$x]; }

Posted: Sat Apr 22, 2006 3:46 pm
by themurph
scratch that... wasn't even aware of str_split