开发者

PHP: How to detect duplicate information processed from file?

I have text file with a bunch of text like:

PLAYER_ENTERED name ipaddress username

but there is other text in here

My code is as follows to extract the ipadress and username:

$q = $_REQUEST["ipladder"];
$f = fopen("ladderlog.txt", "r");
while (($line = fgets($f)) !== FALSE)
{
    if (strstr($line, $q)) 
    {
        $data = explode(" ", $line); // split using the space into an array
                                     // array index 0 = PLAYER_ENTERED
        print "IP:" . $data[1] . "<br>";      // array index 1 = IP
        print "Name: " . $data[2];   // array index 2 = name
    }
}

The output is:
IP: ipaddress

Name: username

My question is... how to I prevent duplicate entries when the same occurrenc开发者_如何学运维e appears in the file?


If the file is not too large and/or this is a local script. I would call file() to get the lines as an array and then array_unique(). Then you could loop over this array to print just the unique items.

$q = $_REQUEST["ipladder"];
$f = file("ladderlog.txt");
$f = array_unique($f);
foreach($f as $line)
{
    if (strstr($line, $q)) 
    {
        $data = explode(" ", $line); // split using the space into an array
                                     // array index 0 = PLAYER_ENTERED
        print "IP:" . $data[1] . "<br>";      // array index 1 = IP
        print "Name: " . $data[2];   // array index 2 = name
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜