我如何检查用户名是否已存在于文件中并将他的积分添加给他?

How can I check if a username already exists in a file and also add his points to him?

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

我当前的代码:

<?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

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

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

你应该使用 PHP_EOL 而不是 "\n" 这也取决于你的 OS

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

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

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

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

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

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

要更新在文本文件中找到的用户,请执行以下操作 link

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

这应该适合你:

首先使用file() to read your file into an array. Then you can use array_map() to loop through each line and explode() it by | as delimiter. After this you can use array_column()获取用户名作为key,点数作为value。像这样:

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 个用户,只需前 10 个元素的 rsort() your array and then take an array_slice(),例如

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

尝试使用preg_match:

$file = fopen("score.txt", "r");

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

但我认为使用 MySQL 更好:)