I have a dynamic text file, and it's content could have fixed lines (repeated once) and x repeated blocks. Each block starts with the same code line "S21.G00.30.001" , but they could haven't the same contents, this is an extract from the content:
S10.G00.00.001,'www.mywebsite.com' //fixed line
S10.G00.00.002,'Company name' // fixed line
S10.G00.00.003,'v2.01' //fixed line
S10.G00.00.005,'02' //fixed line
.............
S21.G00.30.001,'employee one' //block 1
S21.G00.30.002,'AAAA'
S21.G00.30.004,'BBBB'
S21.G00.30.005,'02'
S21.G00.30.006,'16021993'
S21.G00.30.007,'4'
S21.G00.30.008,'A Renasca'
S21.G00 whatever code here ,'some text' // 30 or 40 or 55 ...
S21.G00.30.001,'employee 2' //block 2, S21.G00.30.001 is the divider
S21.G00.30.002,'CCCC'
S21.G00.30.004,'DDDD'
S21.G00.30.005,'02'
S21.G00 whatever code here ,'some text' // 30 or 40 or 55 ...
S21.G00.30.001,'employee 3' //block 3, S21.G00.30.001 is the divider
S21.G00.30.002,'EEEE'
S21.G00.30.004,'FFFF'
S21.G00.30.005,'02'
S21.G00.30.007,'4'
S21.G00.30.008,'some text 3'
S21.G00 whatever code here ,'some text' // 30 or 40 or 55 ...
So, to get the fixed lines values witch are repeated only once, I use this method :
$file = fopen($this->getParameter('dsn_txt_folder') . 'dsn.txt', 'r');
if ($file) {
while (($line = fgets($file)) !== false) {
if (str_starts_with($line, 'S10.G00.00.001')) {
$website = $this->getStringBetween($line, "'", "'");
}
if (str_starts_with($line, 'S10.G00.00.002')) {
$companyName = $this->getStringBetween($line, "'", "'");
}
if (str_starts_with($line, 'S10.G00.00.003')) {
$version = $this->getStringBetween($line, "'", "'");
}
.......
}
fclose($file);
}
But for x repeated blocks , how can I extract each blocks which starts with divider line "S21.G00.30.001" but the end of each block is unknown, and then put each block inside an array, like so I can easly read the values of each line.
The divider or the separator between each block is the line with "S21.G00.30.001".
Finaly , for those 3 blocks, I'd like to get an array like this.
array:1 [▼
0 => array:3 [▼
1 => array:7 [▼
0 => "S21.G00.30.001,'employee one'"
1 => "S21.G00.30.002,'AAAA'"
2 => "S21.G00.30.004,'BBBB'"
3 => "S21.G00.30.005,'02'"
4 => "S21.G00.30.006,'16021993'"
5 => "S21.G00.30.007,'4'"
6 => "S21.G00.30.008,'A Renasca'"
7 => "S21.G00.40.008,'some text'"
8 => "whatever code here,'some text'"
]
2 => array:5 [▼
0 => "S21.G00.30.001,'employee 2'"
1 => "S21.G00.30.002,'CCCC'"
2 => "S21.G00.30.004,'DDDD'"
3 => "S21.G00.30.005,'02'"
4 => "S21.G00.30.006,'16021993'"
5 => "whatever code here,'some text'"
]
3 => array:6 [▼
0 => "S21.G00.30.001,'employee 3'"
1 => "S21.G00.30.002,'EEEE'"
2 => "S21.G00.30.004,'FFFF'"
3 => "S21.G00.30.005,'02'"
4 => "S21.G00.30.007,'4'"
5 => "S21.G00.30.008,'some text 3'"
6 => "whatever code here,'some text'"
]
]
]