Awesome q2a theme

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
26 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 | 26 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,187 answers
0 comments
40,796 users