Answer the question
In order to leave comments, you need to log in
How to copy a file line by line into an array so that each new word is a separate element of the array?
I am trying to copy the file to the array in a loop so that each word in the file becomes an element of the array,
the file itself is
test
test
netest
<?php
$flag=0;
$lines = file('file.txt');
foreach ($lines as $line_num => $line) {
echo $line . "<br />\n";
if ($line=='test')
{
$flag=1;
}
}
echo $flag;
?>
Answer the question
In order to leave comments, you need to log in
Instead of a direct comparison, use one of the following constructs
if (strpos($line, 'test') !== false) { ... } // string contains
if (substr($line, 0, 4 ) === 'test') { ... } // string starts with
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question