How to extract the N word from each line of a text file in PHP

1 Answer

0 votes
function get_second_words() {
    $NWords = array();
    $file = file("data.txt");
    $N = 3;
    foreach ($file as $line) {
        if (trim($line) != '') {
            $expl = explode(" ", $line);
            $NWords[] = $expl[$N - 1];
        }
    }
    return $NWords;
}

echo implode(', ', get_second_words());



/*
run:
 
a, especially, originally, in, implementation
 
*/

 



answered Jul 27, 2020 by avibootz
edited Jul 27, 2020 by avibootz

Related questions

...