So, I want my page to read a txt file and then, tokenize it and create variables like an array.
For example:
My txt file:
Column1/Column2/Column3
A:K:Z
B:U:D
and then I want to read it like $variable['0']['Column1'];
Is there any way to do it?
<?php
$handle = @fopen("info.txt", "r");
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
$delimiters = ":";
$token = strtok($buffer, $delimiters);
$result = [];
$row = [];
$columns = ['rede', 'servidor', 'data', 'hora', 'usuarios', 'servidores'];
while ($token){
$row[$columns[count($row)]] = $token;
if (count($row) == 6) { // write record
$result[] = $row;
$row = [];
}
$token = str_replace('\r', '', strtok($delimiters));
}
print_r($result);
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
?>
$columns?/and:characters? Or, can you expect headers in the middle of the file?