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

0 like 0 dislike
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 | 6 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
0 like 0 dislike
$str = 'test

$arr = explode("\", $str);
110,608 questions
257,186 answers
33,868 users