我有文件集,所以它有像这样的用户名
名1
名称2
NAME3
$tuser = implode(" ", $this->arguments);
$line = file("tready.txt", FILE_IGNORE_NEW_LINES);
$line_check = implode(" ", $line);
$search_string = $tuser;
if (!empty($tuser)) {
if (stripos($line_check, $search_string) !== FALSE) {
$this->say('Your already on the list!');
} else {
file_put_contents("tready.txt", $tuser, FILE_APPEND);
$this->say('Your team has been added to the list: ' . $tuser);
}
} else {
$this->say('Invalid Entry. !Addme [username]');
}
它没有做的是这个:
if (stripos($line_check, $search_string) !== FALSE)
我在脚本运行之前检查了一个echo上的$ line_check和$ search_string并且它们正确显示但是当它们检查自己的某些东西时工作不正常并且它允许脚本无论如何都要添加它们。我使用了错误的程序吗?
答案 0 :(得分:0)
以下是使用array_search()的示例,这使得将字符串与数组匹配变得更加容易。
$tuser = implode(" ", $this->arguments);
$line = file("tready.txt", FILE_IGNORE_NEW_LINES);
if (!empty($tuser)) {
if (array_search($tuser, $line) !== FALSE) {
$this->say('Your already on the list!');
} else {
file_put_contents("tready.txt", $tuser, FILE_APPEND);
$this->say('Your team has been added to the list: ' . htmlentities($tuser));
}
} else {
$this->say('Invalid Entry. !Addme [username]');
}