Я думаю, что существует большое недоразумение об использовании Шаблона "одиночка". Большинство комментариев здесь называет его местом для доступа к глобальным данным. Мы должны быть осторожными здесь - Singleton, как шаблон не для доступа globals.
Singleton должна использоваться для имения только один экземпляр из данного класса. Репозиторий Шаблона имеет большую информацию о Singleton.
If your file uses Windows linebreaks (lines end in \r\n
), you'll get an invisible \r
character at the end of each of your strings. Test for it by running strlen() on one of them:
echo $array[0] . ': ' . strlen($array[0]) . ' chars';
If you get something like
12345: 6 chars
You know that's the problem! You can get rid of these characters after exploding the array using array_map()
with trim()
:
$array = array_map('trim', $array);
Here is what I came up with that worked:
<?php
$file = file_get_contents("common.txt");
$array = explode("\n", $file);
$pass = "snoopy";
if(in_array($pass, $array) == true) {
echo "it's in the array";
}else {
echo "it's not";
}
?>
you may want to use trim
on that too. could be invisible chars you are not seeing by eye.