0

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);
    }
?>
5
  • Are the column headers defined in the text document? Is that what the "Column1/Column2/Column3" is? Or are they defined in your code in $columns? Commented Jan 8, 2019 at 22:58
  • This was just an example. Because this code I tried to, but I couldn't reach what I want. Commented Jan 8, 2019 at 23:00
  • But, the columns should be defined in the PHP, $columns Commented Jan 8, 2019 at 23:00
  • The text file just has things separated by a : Commented Jan 8, 2019 at 23:01
  • We'd need to know the exact format specs in order to figure out edge cases. For instance, how does it escape literal / and :characters? Or, can you expect headers in the middle of the file? Commented Jan 9, 2019 at 7:48

1 Answer 1

1

I wouldn't use strtok. I think it would be easier to just use : as the delimiter in fgetcsv. Something like this:

if ($handle) {
    $columns = ['rede', 'servidor', 'data', 'hora', 'usuarios', 'servidores'];
    while (($row = fgetcsv($handle, 0, ':')) !== false) {
        if (isset($row[5])) {
            $result[] = array_combine($columns, $row);
        }
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

How could I get a value from the first line from column "servidor"?
Should be $result[0]['servidor']
It didn't work, actually, it creates an empty array.
<?php $handle = @fopen("info.txt", "r"); if ($handle) { $columns = ['rede', 'servidor', 'data', 'hora', 'usuarios', 'servidores']; while (($row = fgetcsv($handle, 0, ':')) !== false) { if (isset($row[5])) { $result[] = array_combine($columns, $row); } } } echo $result; ?>
sVipCHAT:IRC.jacksoow.Net:08/01/2019:19:24:51:499:80 sVipCHAT:IRC.NeoStats.Org:08/01/2019:19:25:49:500:80
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.