简体   繁体   中英

is this possible with file get contents?

Hello stackoverflow if i have this log

 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 

Currently this is my code

<?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";
  }

?>

Now what i currently need to do is to make 2 logsearches to be able to grab logs for both players is it possible to just type in Turks as the first variable and Dannys as the second and than search the log and for example make their text a different color?

Hopefully you guys can assist me

Greetings Glenn

EDIT : What i currently have is this

http://gyazo.com/997e6e5bd50cccedb7f6439b013c0c85

So what i basically want is to grab the entire log search for 2 names than mark them with a color New Code

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

Apperently if you fill in more variables it doubles the entire text how can i do it so the text only shows up once and not 5 times

Here's how you could do it:

$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.";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM