As line by line to copy the file into the array, what would each new word was a separate element of the array?


Warning: count(): Parameter must be an array or an object that implements Countable in /home/styllloz/public_html/qa-theme/donut-theme/qa-donut-layer.php on line 274
0 like 0 dislike
8 views
Trying in a loop to copy the array so that each word in the file has become an element of the array
the file itself is a
test test netest

<?php$flag=0;$lines = file('file.txt');foreach ($lines as $line_num => $line) { echo $line . "
\"; if ($line=='test') { $flag=1; } } echo $flag; ?>

Information it displays line by line and it seems all right, but the problem is that if the last word in the file is not "test" the variable flag displays 0 even if the word "test" is in the file, who will able to answer why this is so, because in a loop, display all the words including "test" and the flag should be set to 1?
by | 8 views

2 Answers

0 like 0 dislike
Instead of a direct comparison, use one of the following structures

if (strpos($line, 'test') !== false) { ... } // string contains if (substr($line, 0, 4 ) === 'test') { ... } // string starts with
by
0 like 0 dislike
$str = 'test
test2
test3';

$arr = explode("\", $str);
by
110,608 questions
257,186 answers
0 comments
35,430 users