2014-01-05 93 views
0

你好計算器,如果我有這個日誌這可能與文件獲取內容?

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 fucking 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個logsearches能夠抓住日誌雙方球員是有可能只需輸入Turks作爲第一個變量,Dannys作爲第二個變量,然後搜索日誌並例如使其文本具有不同的顏色?

希望你們能幫助我

問候格倫

編輯:我目前已經是這個

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> 

Apperently如果您在多個變量填寫雙打整個文本我怎麼能做到這一點,使文本顯示時,只有一次,而不是5次

+0

您可能想看看這個:http://stackoverflow.com/questions/2483844/highlight-the-word-in-the-string-if-it-contains-the-keyword – GiamPy

+0

謝謝,我會看看,但IM退出與這些類型的代碼不好:p – Ghermans41

+0

嗯,我有點想知道如何實現它,所以它只是抓住整個文件,而不是突出顯示2名玩家的名字 – Ghermans41

回答

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

確定看起來很有趣你認爲你可以調整我的腳本有點因爲我試圖把它放進去但它並沒有真正的工作 – Ghermans41

+0

Warning:fgets ()期望參數1是資源,在第8行logsearch2.php中給出的字符串我得到這個錯誤 – Ghermans41

+0

你不需要使用file_get_contents。 – GiamPy