用PHP解析日志文件//用数字显示行

I have an odd script that exports data to a log file, the file looks like this:

contactid: 
15186 is present in PND!. contactid: 15186
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
PND Done contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
3630 Is present in PMF!. contactid: 3630
contactid: 
contactid: 
3679 Is present in PMF!. contactid: 3679
3695 Is present in PMF!. contactid: 3695
3699 Is present in PMF!. contactid: 3699
contactid: 
contactid: 
contactid: 
3761 Is present in PMF!. contactid: 3761
contactid: 
3767 Is present in PMF!. contactid: 3767
3770 Is present in PMF!. contactid: 3770
3772 Is present in PMF!. contactid: 3772
contactid: 
3785 Is present in PMF!. contactid: 3785
contactid: 
contactid: 

I've written a script that removes leading contactid: text:

$file1 = $_GET['id'].".log";
$lines = file($file1);  
foreach($lines as $line_num => $line)
{
    $int = trim($line, "contactid: <br/>");
    echo $int;
}

I can then open the file in browser which returns data like this:

15186 is present in PND!. contactid: 15186
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
PND Done contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
contactid: 
3630 Is present in PMF!. contactid: 3630
contactid: 
contactid: 
3679 Is present in PMF!. contactid: 3679
3695 Is present in PMF!. contactid: 3695
3699 Is present in PMF!. contactid: 3699
contactid: 
contactid: 
contactid: 
3761 Is present in PMF!. contactid: 3761
contactid: 
3767 Is present in PMF!. contactid: 3767
3770 Is present in PMF!. contactid: 3770
3772 Is present in PMF!. contactid: 3772
contactid: 
3785 Is present in PMF!. contactid: 3785
contactid: 
contactid: 

What I'm trying to do is remove all empty contactid: rows so my output looks like this:

15186 is present in PND!. contactid: 15186
3630 Is present in PMF!. contactid: 3630
3679 Is present in PMF!. contactid: 3679
3695 Is present in PMF!. contactid: 3695
3699 Is present in PMF!. contactid: 3699
3761 Is present in PMF!. contactid: 3761
3767 Is present in PMF!. contactid: 3767
3770 Is present in PMF!. contactid: 3770
3772 Is present in PMF!. contactid: 3772
3785 Is present in PMF!. contactid: 3785

Stripping down this data has thrown me for a twist! Any help or tips on getting this to work would be great!

You can first load the file using file() with the FILE_IGNORE_NEW_LINES flag to ignore new lines. Then, strip off the whitespace from the beginning and end of the line using trim() and then print the line if the whole line is not equal to contactid:.

$lines = file($file1, FILE_IGNORE_NEW_LINES);  
foreach($lines as $line_num => $line)
{
    if (trim($line) !== 'contactid:') {
        echo $line.'<br/>';
    }
}

Using a regular expression might be easier in this case:

$content = file_get_contents('file.txt');
echo preg_replace('/(contactid:\s*)/', '', $content);

Regex autopsy:

  • () - indicates a capturing group
  • contactid: - matches the characters contactid: literally
  • \s* - matches any white space character zero or more times

The statement means: replace everything that matches contactid:<whitespace> with ''.

Using Amal's code I came up with this that gives me the results I want, but are their ways I can clean up the code by combining the preg_replace():

$content = file_get_contents($file1, FILE_IGNORE_NEW_LINES);
$str = preg_replace('/(contactid: <br\/>\s*)/', '', $content);
$str2 = preg_replace('/(PND Done\s*)/', 'PND Done <br/>', $str);
echo preg_replace('/(PMF Done\s*)/', 'PMF Done <br/>', $str2);

But it is giving me the output I've been looking for...

Thanks!