PHP获取文件内容的问题

时间:2014-01-01 21:22:33

标签: php

我目前遇到的问题是我想获得2个播放器名称,然后像这样显示:

Same TimeStamp
Player1
Player2

不幸的是,我只能这样做

Logs of Player 1
13:04 Player 2 attacked player 1 and dealt 1 damage
-
-
-
-
-
Logs of Player 2
13:04 Player 2 attacked player 1 and dealt 1 damage

当时间戳相同时,我不能让他们互相展示

目前这是我的代码

<?php
$now = date ('m_d_y');
$file = "server_log_".$now.".txt";
$searchfor = ($_POST['name']);
$searchfor2 = ($_POST['name2']);

if ($searchfor !=""){
header('Content-Type: text/plain');

$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/\b.*$pattern.*\b/i"; 

if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches For Player 1:\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found For Player 1";
}
}
else{
  echo "No Name Input";
  }
?>

我想要的是,当搜索两个玩家名称时,它会找到两个日志并且只是在彼此之后显示它们而不是分割日志。

当前播放器2日志仅显示播放器1日志完成时

示例如何不应该

 17:43:37 - Admin TA_Kiki
 17:46:01 - Admin TA_Kiki
 17:47:15 - Admin TA_Kiki
 17:45:28 - SO_Peter has joined the game with ID: 1390151 and has administrator rights

示例应该如何

 17:43:37 - Admin TA_Kiki
 17:45:28 - SO_Peter has joined the game with ID: 1390151 and has administrator rights. 
 17:46:01 - Admin TA_Kiki
 17:47:15 - Admin TA_Kiki

我如何将变量$ searchfor和$ searchfor 2放在同一模式中

在这种情况下

$ searchfor = TA_Kiki $ searchfor2 = SO_Peter

此外,searchfor2必须是一个选项,当searchfor2没有填写任何内容时脚本必须能够避免

1 个答案:

答案 0 :(得分:0)

你可以在打印之前尝试对matches数组进行排序:

    ...
    if(preg_match_all($pattern, $contents, $matches)){
        echo "Found matches For Player 1:\n";
        sort($matches[0]);
        echo implode("\n", $matches[0]);
    }
    ...