Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用preg_match PHP从文本文件中获取值_Php_Regex_Preg Match - Fatal编程技术网

使用preg_match PHP从文本文件中获取值

使用preg_match PHP从文本文件中获取值,php,regex,preg-match,Php,Regex,Preg Match,我有一个以下格式的文本文件 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

我有一个以下格式的文本文件

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
文件已动态生成,我需要获取“.GPS”旁边的“st”(1)值。文本“.GPS.”在每个文件中都是相同的

检查我的以下代码:

$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();
}
没有任何匹配。你知道怎么做吗

(?=.*?\.GPS\.).*?GPS\.\s*(\d+)
试试这个,这个管用

见演示

您可以按照以下步骤操作:

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

如果出现的次数可能超过个,则应使用
preg\u match\u all


如果“.GPS.”不可用,有没有办法达到同样的效果?我的意思是,如果出现其他文本,我怎么能得到相同的结果?@user3008713试试这个。[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\s*\s+\s*(\d+)
preg_match("@\.GPS\.\s+\K(\d+)@is", $f1_content, $match)