2011-04-18 106 views
0

继承人我的代码帮助用PHP数组

$cont = file_get_contents("users.txt"); 
$lines = explode("\n",$cont, true); 
if(in_array('$name', $line)) { 
    echo "Error user $name in database"; 
    exit;} 

我有那么一个文本文件与用户名的文件都被转换为一个数组($线) 我需要它来搜索阵列,看看

$cont = file_get_contents("users.txt"); 
$lines = explode("\n", $cont, true); 
if(in_array($name, $lines)) { 
    echo "Error user $name in database"; 
    exit; 
} 

:在文本文件中

+0

不确定在代码示例中设置$ name变量的位置,但不应该在其中包含单个$引号。 – dkinzer 2011-04-18 04:09:20

+0

现在你总是使用字符串'$ name'而不是一个变量?你的条件应该看起来像“if(in_array($ name,$ lines))” – Nemoden 2011-04-18 04:10:26

回答

0

如果变量$ name是名称的字符串,你想你从文件创建数组应该看起来更像这找到用户名我喔我个人建议去正则表达式的路由。此外,随着文件的增长,我可以看到“爆炸”成为内存耗尽,并可能大幅减缓网站速度。

我还没有深入研究过这个问题,但是如果你打算使用平面文件,那么如果系统中会有大量的用户,你可能想沿着http://www.niblr.com/php-flat-file-search-script/的方向尝试一些东西。甚至只是针对file_get_contents的结果运行常规表达式。