Search code examples
phpregexpreg-match

Fetch value from text file using preg_match PHP


I have a text file in following format.

Wed Aug 27 20:24:53.536 IST

      address         ref clock     st  when  poll reach  delay  offset    disp
*~172.16.18.163    .GPS.             1   657  1024  377   13.99   1.801  19.630
 ~127.127.1.1      .LOCL.            3    15    64  377    0.00   0.000   1.920
 * sys_peer, # selected, + candidate, - outlayer, x falseticker, ~ configured

file has been generated dynamically I need to fetch value of 'st' (1) value next to '.GPS.' . text '.GPS.' is going to be same in every file.

Check my following code:

$f1_content = file_get_contents('filename.txt');
if(preg_match("/\b(.*)\s(.*)\s[0-9]\s\b/i", $f1_content, $match))
{
                print_r($match); die();
}

not getting any match. Any idea how it can be done?


Solution

  • (?=.*?\.GPS\.).*?GPS\.\s*(\d+)
    

    Try this.This works.

    See demo.

    http://regex101.com/r/pP3pN1/12