Search code examples
codeigniterxcode-instruments

Read a file codeigniter


I need to read this file:

AVRAMOV P   ATALANTA    1   1   0
FREZZOLINI  P   ATALANTA    1   1   0
SPORTIELLO  P   ATALANTA    1   16  15
BIAVA   D   ATALANTA    8   9   1
EMANUELSON  D   ATALANTA    7   5   -2
STENDARDO   D   ATALANTA    7   9   2
BENALOUANE  D   ATALANTA    6   8   2
DRAME'  D   ATALANTA    5   5   0

I read this file, but I need to have some operation which I can work on string. Anyone can suggest me any method?


Solution

  • It's hard to tell what kind of string operation you're looking to do, but if you want to split the fields, you could do something like this:

    <?php
    $res = fopen('myfile.txt', 'r');
    $data = array();
    
    while (($line = fgets($res)) !== false) {
        $data[] = preg_split('/\s{2,}/', $line);
    }
    
    fclose($res);
    

    This will create an array of lines, each line containing an array of fields separated by two or more spaces, for example:

    [
        [
            'AVRAMOV',
            'P',
            'ATALANTA',
            '1',
            '1',
            '0'
        ],
        [
            'FREZZOLINI',
    // ...