skippylou Posted January 9, 2008 Share Posted January 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
skippylou Posted January 9, 2008 Author Share Posted January 9, 2008 Found my error. I was debugging using my web interface, which html reduces number of whitespace down to 1 - when in actuality, my file had 5, 6, 8, etc. spaces between fields. Changed " " to \s* in my pattern, all good now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.