I want to split a string of 84 characters into an array of 14 elements, each containing 6 characters.

example:

string = 123456789123456789 (etc.)

will return

array[0] = 123456;
array[1] = 789123;
array[3] = 456789;

etc....

I've tried this (and several variations)

$split = preg_split(' {6}', $station_data); 

This has to be simple, but I can't get it working right.... GRRRR...

Thanks-
James

    [man]chunk_split[/man]

    If you want an array after that, just explode on the \r\n.

      or [man]str_split[/man] might work better in your case

        Originally posted by mtimdog
        or [man]str_split[/man] might work better in your case

        You'd still have to make it manual unless you're running PHP5...

          if (!function_exists('str_split')) {
            Function str_split($string, $chunksize=1) {
             preg_match_all('/('.str_repeat('.', $chunksize).')/Uims', $string, $matches)
          ;
             return $matches[1];
            }
          } 
          

          from the man

            Hrrmmm....I think I'd still chunk split it.

            $split = chunk_split($num,6);
            $array = explode("\r\n",$split);
            

              str_split is for PHP 5 only, but would have worked perfectly 🙂

              chunking then exploding worked! Thanks, I must be working too hard 😉

                Originally posted by LordShryku
                Hrrmmm....I think I'd still chunk split it.

                $split = chunk_split($num,6);
                $array = explode("\r\n",$split);
                

                [/B]

                Ok, but the last thing in your array will be "" because chunksplit adds \r\n at the end too.

                  Write a Reply...