Search code examples
phpirc

stripos Not working


I have the file set so that it has the usernames like so

Name1

Name2

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]');
}

What its not doing is this:

if (stripos($line_check, $search_string) !== FALSE)

I have check the $line_check and $search_string on an echo prior to the script running and they show up correctly but when it comes to having them check against themselves somethings not working properly and it allows the script to add them anyway. Am I using the wrong procedures?


Solution

  • Here is an example using array_search(), which makes it much easier to match the string against the array.

    $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]');
    }