[thelist] PHP, 8 digit string, to 8 item array..

Christian Heilmann codepo8 at gmail.com
Tue Jan 16 11:02:04 CST 2007


> Tris wrote:
> > I wanna take an Int (eg: 48392034)
> > and make an array:
> > $myArray[0] = 4;
> > $myArray[1] = 8;
> > $myArray[2] = 3;
> > ...
>
> // PHP5 only:
> str_split((string)48392034,1);
>
> // PHP4+ string access by brackets
> $intString = (string)4892034;
> $myArray = array();
> for( $i=0; $i<strlen($intString); $i++ ) {
>   $myArray[] = $intString{$i};
> }

Or with an empty regEx:
$s = '1213213';
$myArray = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
print_r($myArray);

http://uk2.php.net/manual/en/function.preg-split.php (Example 2)

-- 
Chris Heilmann
Book: http://www.beginningjavascript.com
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/



More information about the thelist mailing list