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

1 Answer

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

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



/*
run:

is, is, was, Lerdorf, reference

*/

 



answered Jul 27, 2020 by avibootz

Related questions

1 answer 217 views
1 answer 188 views
1 answer 226 views
1 answer 218 views
1 answer 197 views
...