[thelist] preg_split in PHP

noah noah at tookish.net
Mon Jan 14 22:30:23 CST 2002


At 08:55 PM 14/01/2002, alpha at student.uci.agh.edu.pl wrote:
>On Mon, Jan 14, 2002 at 08:33:35PM -0500, noah wrote:
> > string is just a number (e.g., 2457) and I want to split it into 
> individual
> > digits.
> > $acts = preg_split('//', '2457');
>
>Why do you use split for such task? String is an array, so you can
>easily access each digit by its index in the string:

Thanks for that.

It worked, but I still wasn't able to count the characters properly 
(preg_split was still acting funny, and count_chars() seems to have emerged 
post 4.0b2, though php.net says it's in 4.0.0 and above).

Fortunately I'm still at a stage where redesigning the data is an option, 
so I reformatted the data to a comma separated list - now I can use 
explode(), and count works properly.

I'm still curious about why when I did:

$acts = preg_split('//', '2457');
$number_of_acts = (count($acts));

$number_of_acts came back as 6.

It doesn't matter anymore, but it still seems pretty strange. I'm sure it's 
not the quotations, by the way, since '2357', 2457, and "2457" all came 
back the same way.

Cheers,
Noah





More information about the thelist mailing list