2015-05-04 226 views
0

我有这样的搜索脚本中筛选出特定的字符串:一个txt文件

$search = $_GET["search"]; 
    $logfile = $_GET['logfile']; 
    $file = fopen($logfile, "r"); 

?> 
<head> 
    <title>Searching: <?php echo $search ?></title> 
</head> 
<?php 

    while(($line = fgets($file))!= false) { 
     if(stristr($line,$search)) { 
      // case insensitive 
      echo "<font face='Arial'> $line </font><hr><p>";         
     } 
    } 

我想筛选出特定字符串为txt文件的东西搜索时。

例如,文本文件包含此:

http://test.com/?id=2022458&pid=41&user=Ser_Manji 
Ser_manji said "hello" 
Ser_manju left the game 

当您搜索例如用于“Ser_manji”,我想筛选出这串:

http://test.com/?id=2022458&pid=41&user=Ser_Manji 

但仍显示这些两行:

Ser_manji said "hello" 
Ser_manju left the game 

我希望这是可能的,我自己tryied改变它,所以它不会接受与包含“test.com”的行有关,但没有奏效。

+0

仅供参考:您可以接受最能帮助您的答案并解决您的问题(http://meta.stackexchange.com/q/5234)! – Rizier123

回答

1

这应该为你工作:

只是让你的文件到一个数组与file()。并使用strpos()检查搜索针是否在线中,是否显示线。

<?php 

    $search = $_GET["search"]; 
    $logfile = $_GET['logfile']; 
    $lines = file($logfile, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); 

?> 

<head> 
    <title>Searching: <?php echo $search ?></title> 
</head> 

<?php 

    foreach($lines as $line) { 
     if(strpos($line, $search) === FALSE) { 
      echo "<font face='Arial'>$line</font><hr><p>";  
     } 
    } 

?> 
+0

对不起,我对这类PHP的工作都很陌生。但是,它在哪里允许我特别只过滤“test.com”,而不是显示整个行,并显示其余不包含“test.com”的其余部分。 – Codah3

+0

@ Codah3所以,你现在想显示每一行,但只是删除行中的某些文本?或者只有在行中找不到特定文本时才显示该行? – Rizier123

+0

@ Codah3那么,我们现在在哪里与这个问题? – Rizier123

0

你只需要修改你的病情是否像这样:

if (stristr($line, $search) && strpos($line,'test.com') === false) 
0

我想你需要根据特定的用户名来过滤掉日志。这似乎比找到正确的php函数更复杂。

所以你得到了你的搜索q:$search = $_GET['search']这是一个用户名。你得到你的日志文件:$file = fopen($logfile, 'r')

请注意:您使用GET参数来获取文件名,但您的示例链接http://test.com/?id=2022458&pid=41&user=Ser_Manji不包含任何&logfile=logs.txt。我想你知道你在做什么。

现在,如果您的日志结构是{用户名} {操作},那么我们知道“空间”会将用户名与他的操作分开。我们可以使用爆炸:$clues = explode(' ', $line);,现在$username = $clues[0]$action = clues[1]

所以if ($username == $search) echo $action

保持简单和干净。

$search = $_GET["search"]; 
$logfile = $_GET['logfile']; 
$file = fopen($logfile, "r"); 

while ($line = fgets($logfile)) { 
    $clues = explode(' ', $line); 
    $username = $clues[0]; 
    $action = $clues[1]; 
    if ($username == $search) { 
     echo $action; 
    } 
} 

你应该测试:如果你正在寻找logs.txt文件中user_1234等http://test.com?search=user_1234&logfile=logs.txt ..

0

如果你只想在的开头匹配文本(不区分大小写)线,则可以考虑使用不敏感的情况下和anchored正则表达式,用于在阵列与preg_grep功能理想滤波上的文本文件(例如,经由file),或与上SplFileObject一个FilterIterator

// regular expression pattern to match string at the 
// beginning of the line (^) case insensitive (i). 
$pattern = sprintf('~^%s~i', preg_quote($search_term, '~')); 

对于数组的变体:

$result = preg_grep($pattern, file($logfile)); 
foreach ($result as $line) { 
    ... // $line is each grep'ed (found) line 
} 

随着迭代器是稍有不同:

$file = new SplFileObject($logfile); 
$filter = new RegexIterator($file, $pattern); 
foreach ($filter as $line) { 
    ... // $line is each filtered (found) line 
} 

的迭代器给你一个更面向对象的方法,该阵列的感觉也许更直截了当。这两种变体都使用PHP中的PCRE正则表达式运行,这是PHP中的标准正则表达式方言。