Why is this code not working?
I have test.txt it contains:
A
B
C
My PHP code is:
$arr = file('test.txt');
if (in_array('A', $arr)) {
echo 'A is found';
}
Result. nothing. But this following code works fine:
if (in_array('C', $arr)) {
echo 'C is found';
}
Can someone help me?
Because file()
will not remove the newline symbols from the end of each line, if you don't tell him to do so
$arr = file('test.txt', FILE_IGNORE_NEW_LINES);
or remove it yourself, what will remove any other whitespace from the beginning and end of each line
$array = array_map('trim', file('test.txt'));