Jump to content

[SOLVED] Help with preg_match with file reads


skippylou

Recommended Posts

So I have this preg_match problem that I am hoping someone can help me with.

 

I have a file that I am reading in that contains lines like such:

 

12:00:05.609540 6 10.1.1.100.62448 -> 10.1.1.200.80 74 0 REQ

 

In a php script I have a chunk of code like:

 

$fh = fopen($procfile,'r');

while (!feof($fh)) {

  $data = fgets($fh);

  $data = trim($data);

  if (preg_match("/(\d\d:\d\d:\d\d\.\d+) (\d)+ (\d+\.\d+\.\d+\.\d+)\.(\d+) ([\<\-\>]+) (\d+\.\d+\.\d+\.\d+)\.(\d+) (\d+) (\d+) (\w+)/", $data, $matches)) {

    echo "blah";

  }

}

 

This isn't working. However the following two things do work or shed light on parts that are working:

 

1) If I just put: echo $data;  outside of the if statement, I see all the lines with their spaces trimmed, etc.  So this proves the file read is happening and it is removing whitespace, etc.  The lines look good.

2) If I change the if statement to match a static example of a sample line from the file, instead of $data, it works:

 

if (preg_match("/(\d\d:\d\d:\d\d\.\d+) (\d)+ (\d+\.\d+\.\d+\.\d+)\.(\d+) ([\<\-\>]+) (\d+\.\d+\.\d+\.\d+)\.(\d+) (\d+) (\d+) (\w+)/", "12:00:05.609540 6 10.1.1.100.62448 -> 10.1.1.200.80 74 0 REQ", $matches)) {

    echo "blah";

  }

 

With this above, I get a "blah" printed for every line in the file.  So this proves the regex is working.

 

I have also tried double-quoting ("$data"), the $data variable to see if that was it - but with no luck.

 

Can anyone shed any light on this, or have any ideas where my errors are?

 

Thanks,

 

Scott

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.