这可能与文件获取内容?

时间:2014-01-05 15:14:51

标签: php

Hello stackoverflow如果我有这个日志

 16:23:36 - [Turks] dannys 
 16:23:55 - *LOCAL* [Turks] stop! 
 16:24:08 - *LOCAL* [Turks] stop 
 16:24:11 - *LOCAL* [Turks] no attack 
 16:24:28 - *LOCAL* [Turks] stop! 
 16:24:35 - *LOCAL* [Turks] hey 
 16:24:37 - *LOCAL* [Turks] dur amk 
 16:24:52 - *LOCAL* [Dannys] fottiti 
 16:25:04 - *LOCAL* [Turks] hey 
 16:25:11 - *LOCAL* [Turks] hey 
 16:25:12 - *LOCAL* [Turks] money 
 16:25:36 - Turks <img=ico_swordtwo> Dannys 
 16:25:43 - [Turks] you noob 
 16:26:09 - *LOCAL* [Turks] I'm giving your money! 
 16:26:19 -  <img=ico_headshot> Turks 
 16:26:46 - Dannys has joined the game with ID: 416519 
 16:26:46 - Dannys has joined the game with ID: 416519 
 16:26:46 - Dannys has joined the game with ID: 416519 
 16:26:46 - Dannys has joined the game with ID: 416519 

目前这是我的代码

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

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 ".$searchfor.":\n";
   echo implode("\n", $matches[0]);
}
else{
   echo "No matches found For Player ".$searchfor."";
}
}
else{
  echo "No Name Imput";
  }

?>

现在我需要做的是让2个日志搜索能够为两个玩家抓取日志是否可以只输入土耳其人作为第一个变量而Dannys作为第二个变量而不是搜索日志,例如make他们的文字颜色不同?

希望你们能帮助我

问候格伦

编辑:我目前拥有的是这个

http://gyazo.com/997e6e5bd50cccedb7f6439b013c0c85

所以我基本上想要的是获取2个名称的整个日志搜索,而不是用颜色标记它们 新代码

<html>
<head>
<style>
body {
white-space:pre-wrap;
font-family: TimesNewRoman, "Times New Roman", Times, Baskerville, Georgia, serif;
font-size: 14px;
color: black;
background-color: white;
}
</style>
</head>
<body>
<?php
error_reporting(E_ERROR | E_PARSE);
$now = ($_POST['date']);
$player = ($_POST['player']);
$player2 = ($_POST['player2']);
$player3 = ($_POST['player3']);
$player4 = ($_POST['player4']);
$player5 = ($_POST['player5']);


if ($player != ""){
$handle = fopen("logs/server_log_".$now.".txt", "r");
if ($handle)
{
    while (($line = fgets($handle)) !== false)
    {
        echo preg_replace("/\w*?". preg_quote($player) ."\w*/i", "<span style=\"color: red;\">$0</span>", $line) . '';
        if ($player2 !=""){
        echo preg_replace("/\w*?". preg_quote($player2) ."\w*/i", "<span style=\"color: blue;\">$0</span>", $line) . '';
        }
        if ($player3 !=""){
        echo preg_replace("/\w*?". preg_quote($player3) ."\w*/i", "<span style=\"color: yellow;\">$0</span>", $line) . '';
        }
        if ($player4 !=""){
        echo preg_replace("/\w*?". preg_quote($player4) ."\w*/i", "<span style=\"color: green;\">$0</span>", $line) . '';
        }
        if ($player5 !=""){
        echo preg_replace("/\w*?". preg_quote($player5) ."\w*/i", "<span style=\"color: purple;\">$0</span>", $line) . '';
        }
    }
} 
else 
{
    echo "Error in opening the file.";
}
}
else {
echo "You must fill in 1 player name";
}
?>
</body>
</html>

如果你填写更多的变量,它会使整个文本加倍,我怎么能这样做,所以文本只显示一次而不是5次

1 个答案:

答案 0 :(得分:0)

以下是你如何做到的:

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

if ($handle)
{
    while (($line = fgets($handle)) !== false)
    {
        echo preg_replace("/\w*?". preg_quote('Turks') ."\w*/i", "<span style=\"color: red;\">$0</span>", $line) . '<br/>';
    }
} 
else 
{
    echo "Error in opening the file.";
}