Difference between fgetcsv and fgets and speed difference

I was going to read text files trying to read the files as quickly as possible and put them in an array line by line.

Some of the files I am using are .csv files and some of them are .txt files. When I started, I found an example for reading csv files, so I started using it, but quickly found that fgets was significantly faster. FgetcsvValues ​​is the first one I created and fgetsValues ​​is the second / faster one.

I am injecting the files through the first $ chars parameter as an array. So, for example, this will be the array I am using

$dictionaryfiles = array(
    'dictionaries/Dutch.csv'
    ,'dictionaries/English.txt);

      

The problem I am facing is that the first character of each line becomes NULL if I use the fgetsValues ​​function I created. Is this caused by the .csv file type or am I just doing something wrong?

The two functions are not exactly the same because I read that going through an array using FOR instead of FOREACH is faster / uses less memory, but other than that they are almost the same.

The substr function is used to truncate the last bit of text on each line. An example of a word would be "Hello / 5". The first part is a word, and / is used as a separator to display the number of characters.

And finally, I use array_unique to remove any duplicate words

function fgetValues($chars){
    $multilines = array();
    $c = count($chars);
    for ($i=0; $i<$c; $i++){
        $actualloc = 'data/'.$chars[$i].'';
        $file = fopen($actualloc, 'r');
        while(($line = fgets($file)) !== FALSE ) {
            $line[0] = trim(substr($line[0], 0, strpos($line[0], "/")));
            array_push($multilines, $line);
        }
        fclose($file);
    }
    $multilines = array_unique($multilines,SORT_REGULAR);
    return $multilines;  
}

function fgetcsvValues($chars){
    $resultlines = array();
    foreach($chars as $single){
        $actualloc = 'data/'.$single.'';
        $file = fopen($actualloc, 'r');
        while(($line = fgetcsv($file)) !== FALSE) {
            $line[0] = substr($line[0], 0, strpos($line[0], "/"));
            array_push($resultlines, $line);
        }
        fclose($file);
    }
    $resultlines = array_unique($resultlines,SORT_REGULAR);
    return $resultlines;  
}

      

What causes the first characters to become NULL values? And how can I improve any of the functions to read text files faster or better?

+3


source to share





All Articles