【问题标题】:stripos Not working条纹不工作
【发布时间】:2014-04-24 22:28:25
【问题描述】:

我已经设置了文件,因此它具有这样的用户名

姓名1

名字2

名字3

$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)

在脚本运行之前,我已经检查了 $line_check 和 $search_string 的回显,它们显示正确,但是当它们检查自己时,某些东西不能正常工作,它允许脚本无论如何添加它们。我是否使用了错误的程序?

【问题讨论】:

  • 当您可以使用 file() 并与 array_search() 匹配时,为什么要进行所有字符串恶作剧

标签: php irc


【解决方案1】:

这是一个使用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]');
}

【讨论】:

  • 我不太明白您所说的“按原样检查/添加”是什么意思,我需要从命令中提取数据,$tuser 不能是静态短语。
  • 只要 $tuser 以字符串结尾就可以了。 $tuser = implode(" ", $this->arguments);无需重新分配
  • 哦,IC 是的,我这样做只是为了进行检查,这样做真的没有任何功能目的。不幸的是,代码仍然跳过检查并仍然将字符串添加到文件中。
  • 没关系,这是一个新问题,现在它不会将它添加到新行:(
  • 如果你想要一个新的行然后添加一个file_put_contents("tready.txt", $tuser.PHP_EOL.PHP_EOL, FILE_APPEND); 所以将 PHP_EOL 或 "\n\n" 附加到字符串
猜你喜欢
  • 2015-10-07
  • 1970-01-01
  • 2021-11-24
  • 2021-08-01
  • 1970-01-01
  • 2020-12-13
  • 1970-01-01
  • 1970-01-01
  • 2015-05-25
相关资源
最近更新 更多