The problem is, that you have a new line character at the end of each line, so:
test
!== test
//^^ See here
That's why it doesn't work as you expect it to.
How to solve it now? Can I introduce to you the function: file()
. You can read a file into an array and set the flag to ignore these new lines at the end of each line.
So putting all this information together you will get this code:
$array = file("countries.txt", FILE_IGNORE_NEW_LINES);
$str = "test";
foreach ($array as $key => $val) {
if ($val == $str) {
echo $val;
} else {
echo "not found";
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…