<?php function checklongest($sentence) { $longest = ""; $word_array = explode(' ', $sentence); for ($i = 0; $i < count($word_array); $i++) { if (strlen($word_array[$i]) > strlen($longest)) { $longest = $word_array[$i]; } } echo $longest; } $fh = fopen($argv[1], "r"); while (!feof($fh)) { $sentence = trim(fgets($fh)); # //call a function to check the longest word checklongest($sentence); echo "\n"; # break loop if $test is an empty line # $test represents the test case, do something with it }
if (count($longest) == $number) { break; } } foreach ($longest as $longest) { echo $longest; echo "\n"; } // for($i=1; $i!=count($string); $i++){ // if($i == 1){ // $longest[] = $string[$i]; // } // else($s) // } // if($longest){ // foreach($longest as $longest){ // echo $longest; // echo "\n"; // } // } // else{ // echo "Error"; // } } $string = array(); $fh = fopen($argv[1], "r"); while (!feof($fh)) { $string[] = trim(fgets($fh)); } checklongest($string);