user3008713
user3008713

Reputation:

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?

Upvotes: 0

Views: 253

Answers (2)

revo
revo

Reputation: 48711

You can follow this:

@\.GPS\.\s+\K(\d+)@is

PHP:

preg_match("@\.GPS\.\s+\K(\d+)@is", $f1_content, $match)

If you may have more than of occurrences of that, you should use preg_match_all

Live demo

Upvotes: 1

vks
vks

Reputation: 67968

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

Try this.This works.

See demo.

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

Upvotes: 0

Related Questions