【问题标题】:How can I check if a username already exists in a file and also add his points to him?如何检查文件中是否已存在用户名并将其积分添加给他?
【发布时间】:2015-10-13 18:08:14
【问题描述】:

我有意使用文本文件来执行此操作。所以我读取了一个文本文件,我想检查该文本文件中是否已经存在用户名,如果他不存在,我想将此用户名添加到文本文件中,或者只是将点添加给他。

我当前的代码:

<?php
$myfile = fopen("test.txt", "r") or die("Unable to open file!");
$file = fread($myfile,filesize("test.txt"));
//echo $file;
fclose($myfile);

//$username = $_REQUEST['username'];
//$points = $_REQUEST['point'];
$username = 'chinmay'; //chinmay is a username this is unique
$points = 200; //if username chinmay not exitst then Insert first time otherwise if username chimay exist then next onwards this point will update everytime in the text file.

$myfileWrite = fopen("test.txt", "a") or die("Unable to open file!");
$txt = $username."|".$points."\n";
fwrite($myfileWrite, $txt);

fclose($myfileWrite);
?> 

test.txt:

chinmay|800
john|200
sanjib|480
debasish|541

这是我的完整代码。我的要求是:

  • \n 在我使用插入同一行的文本时不起作用。
  • 如何检查重复的用户名?
  • 如果我找到了用户名,那么如何更新用户积分?

我用谷歌搜索了过去 2 个小时,但没有得到任何解决方案。我不知道这个问题。

【问题讨论】:

  • 我不想使用任何数据库。因为我的数据库负载很重。另一个应用程序正在运行我的数据库。查询在一分钟内执行了 1000 次以上。所以我想使用文本文件。
  • 文本文件的效率会降低。
  • 我知道这效率较低。但这是暂时的。我将开始一个从今天起持续 7 天的比赛。所以我不需要对我的数据库施加任何压力。我得到Too many connection in my database的最长时间@
  • Flat File Databases 的可能重复项
  • 您根本没有实施任何错误检查/处理。这是故意的吗?

标签: php file fwrite fread


【解决方案1】:

你应该使用 PHP_EOL 而不是 "\n" 这也取决于你的操作系统

$txt = $username."|".$points.PHP_EOL;

要检查用户名,只需使用:

//this works because $file is String because of fread()
if (strpos($file,$username) !== false) {
    echo 'user exists';
}

要替换,您需要正则表达式或使用 strpos 位置(返回字符串中名称的位置)并将指针前进 count($username)+1 并从那里搜索换行符,在这之间的所有字符串,替换为新点

【讨论】:

  • PHP_EOL 常量是经常给出的建议,但我的印象是很多人都弄错了常量。它并不能神奇地解决所有问题。对于手头的问题,用于编写和分解文件内容,这是一个优雅的解决方案。但它并没有解决看似没有写换行符的 OPs 问题。
【解决方案2】:

要让 \n 正常工作,请使用 PHP_EOL 作为另一个答案

$txt = $username."|".$points.PHP_EOL;

要更新在文本文件中找到的用户,请通过以下链接

how to replace a particular line in a text file using php?

【讨论】:

    【解决方案3】:

    这应该适合你:

    首先使用file() 将您的文件读入一个数组。然后,您可以使用array_map() 循环遍历每一行,并使用explode()| 作为分隔符。在此之后,您可以使用array_column() 将用户名作为键,将点作为值。像这样:

    Array
    (
        [chinmay] => 1200
        [john] => 200
        [sanjib] => 480
        [debasish] => 541
        [chinmayx] => 200
    )
    

    使用数组,您可以简单地检查用户名是否已经存在。如果不将其添加到数组中,然后将点添加到它。

    将点添加到用户名后,您可以将数据改回相同的格式并使用file_put_contents() 保存。

    完整代码:

    <?php
    
        $lines = file("test.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        $usernames = array_column(array_map(function($v){
            return explode("|", $v);
        }, $lines), 1, 0);
    
        $username = "chinmayx";
        $points = 200; 
    
        if(!isset($usernames[$username]))
            $usernames[$username] = 0;
        $usernames[$username] += $points;
    
        foreach($usernames as $k => $v)
            $data[] = "$k|$v" . PHP_EOL;
    
        file_put_contents("test.txt", $data);
    
    ?>
    

    编辑:

    如果您的 PHP 低于 5.5,只需替换:

    $usernames = array_column(array_map(function($v){
        return explode("|", $v);
    }, $lines), 1, 0);
    

    用这个:

    $lines = array_map(function($v){
        return explode("|", $v);
    }, $lines);
    
    $usernames = array_combine(
        array_map(function($v){
            return $v[0];
        }, $lines),
        array_map(function($v){
            return $v[1];
        }, $lines)
    );
    

    此外,如果您想获得前 10 名用户,只需 rsort() 您的数组,然后从前 10 个元素中取一个 array_slice(),例如

    rsort($usernames);
    $topUsers = array_slice($usernames, 0, 10);
    print_r($topUsers);
    

    【讨论】:

    • 出现错误。 Fatal error: Call to undefined function array_column() in E:\xampp\htdocs\2015\demoProject\index.php on line 3
    • @Chinu echo phpversion(); 可能会给你低于 5.5 的东西。 对吗?!
    • 我的php版本是5.3.5
    • @Chinu 然后将第 3 行:$usernames = array_column(array_map(function($v){ return explode("|", $v); }, $lines), 1, 0); 替换为 $usernames = array_combine( array_map(function($v){ return $v[0]; }, $lines), array_map(function($v){ return $v[1]; }, $lines) );
    • :( :( Warning: array_combine() [function.array-combine]: Both parameters should have at least 1 element in E:\xampp\htdocs\2015\demoProject\index.php on line 9
    【解决方案4】:

    尝试使用 preg_match:

    $file = fopen("score.txt", "r");
    
    while (!feof($file)) {
    preg_match("/^$username|(.*?)$/", $file, $array);
    var_dump($array);
    }
    

    但我认为最好使用 MySQL :)

    【讨论】:

      猜你喜欢
      • 2019-05-24
      • 2013-05-21
      • 2021-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-17
      • 2016-08-08
      • 1970-01-01
      相关资源
      最近更新 更多