heykarch
heykarch

Reputation: 39

convert a txt file into array with php

how would you take the following text from file and turn into an array? below is the code i use to read the file but right now it only puts everything into one array

<?php
$fileString = file_get_contents("test.txt");
  //print_r($fileString);

  //$words = preg_split("/^0\s*$/m", $fileString);
  //print_r($words);
  $rows = explode("\r", $fileString);
  print_r($rows);
 ?>

test.txt below

0
CIRCLE
8
V_DrillSF_16
39
16
10
765.1
20
57.0
30
0
40
4.0
0
CIRCLE
8
V_DrillSFS
39
16
10
765.1
20
57.0
30
0
40
3.5
0
CIRCLE
8
V_DrillSF_16
39
16
10
765.1
20
89.0
30
0
40
4.0
0
CIRCLE
8
V_DrillSF_16
39
16
10
765.1
20
249.0
30
0
40
4.0

i would like to break up or identify each "CIRCLE" grouping as its own array so that i can then add it to a database each "CIRCLE" grouping starts with a 0 and has the same number of rows each time.

[0] => Array
(
[0]=>0
[1]=>CIRCLE
[2]=>8
[3]=>V_DrillS_16
[4]=>39
[5]=>16
[6]=>10
[7]=>765.1
[8]=>20
[9]=>57.0
[10]=>30
[11]=>0
[12]=>40
[13]=>4.0
)

Upvotes: 1

Views: 99

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626920

You can split the file contents at the locations where 0\nCIRCLE appears, and then explode each chunk with a line break (sequence) (you can again use a regex, or just explode).

Here is an example with CRLF line breaks:

$fileString = "0\r\nCIRCLE\r\n8\r\nV_DrillSF_16\r\n39\r\n16\r\n10\r\n765.1\r\n20\r\n57.0\r\n30\r\n0\r\n40\r\n4.0\r\n0\r\nCIRCLE\r\n8\r\nV_DrillSFS\r\n39\r\n16\r\n10\r\n765.1\r\n20\r\n57.0\r\n30\r\n0\r\n40\r\n3.5\r\n0\r\nCIRCLE\r\n8\r\nV_DrillSF_16\r\n39\r\n16\r\n10\r\n765.1\r\n20\r\n89.0\r\n30\r\n0\r\n40\r\n4.0\r\n0\r\nCIRCLE\r\n8\r\nV_DrillSF_16\r\n39\r\n16\r\n10\r\n765.1\r\n20\r\n249.0\r\n30\r\n0\r\n40\r\n4.0";
$res = preg_split('~(*ANYCRLF)\R(?=0\RCIRCLE$)~m', $fileString);
print_r(array_map(function($x) { return explode("\r\n", $x); }, $res));

See the PHP demo.

Regex details:

  • (*ANYCRLF) - \R now matches CRLF, LF or CR line endings
  • \R - one or more line break sequences
  • (?=^0\RCIRCLE$) - a positive lookahead that matches
    • ^ - start of a line (due to m, ^ matches start of lines)
    • 0 - a 0
    • \R - a single line break sequence
    • CIRCLE - CIRCLE string
    • $ - end of a line.

Upvotes: 1

Related Questions