我正在使用 in_array
在数组中搜索值是否存在。我的代码如下:
$file = 'domains.txt'; //reading txt file
$lines = file($file);//file in to an array
if (in_array("yahoo.com", $lines)) {
echo "Got Domain";
}
但是,它不起作用,它只显示一个空白页面,但 yahoo.com 在domains.txt中可用,所以它应该返回 true,但我坚持下去,请帮助,谢谢。
最佳答案
调用file()
时添加FILE_IGNORE_NEW_LINES
标志:
$lines = file($file, FILE_IGNORE_NEW_LINES);
当读取 yahoo.com 行时,它被存储为:
"yahoo.com\n"
上面的标志将告诉 file()
不要将换行符 (\n
) 附加到返回数组中的每个字符串。
关于php - in_array 函数未给出预期结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10235767/