fork(3) download
  1. <?php
  2.  
  3. function split_columns ($string, $indices) {
  4. $pat = "";
  5. foreach ($indices as $key => $id) {
  6. if ($key==0) {
  7. $pat .= "(.{" . $id . "})";
  8. } else if ($key<count($indices)) {
  9. $pat .= "(.{" . ($id-$indices[$key-1]) . "})";
  10. }
  11. }
  12. $pats = '~^'.$pat.'(.*)$~m';
  13. preg_match_all($pats, $string, $arr);
  14. return array_slice($arr, 1);
  15. }
  16. $string = "11234567891234567\n11234567891234567"; // 1: '1', 2: '123456789', 3: '1234', 4: '567'
  17. print_r (split_columns($string, $positions=array(1, 10, 14)));
Success #stdin #stdout 0.01s 52488KB
stdin
Standard input is empty
stdout
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => 1
        )

    [1] => Array
        (
            [0] => 123456789
            [1] => 123456789
        )

    [2] => Array
        (
            [0] => 1234
            [1] => 1234
        )

    [3] => Array
        (
            [0] => 567
            [1] => 567
        )

)