How to extract the last 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[count($expl) - 1];
        }
    }
    return $firstWords;
}

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



/*
run:

language , development , programmer , the , Group

*/

 



answered Jul 27, 2020 by avibootz

Related questions

1 answer 218 views
1 answer 182 views
1 answer 226 views
1 answer 218 views
1 answer 198 views
...