我的文本文件包含一堆文字,如:
PLAYER_ENTERED name ipaddress username
但是这里还有其他文字
我的代码如下提取ipadress和用户名:
$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
}
}
输出结果为:
IP:ipaddress
名称:用户名
我的问题是......当文件中出现同一事件时,如何防止重复输入?
答案 0 :(得分:1)
如果文件不是太大和/或这是本地脚本。我会调用file()
将行作为数组,然后array_unique()
。然后你可以遍历这个数组来打印唯一的项目。
$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
}
}