如何从php行打印字符串


How to print string from line php?

我需要一些帮助我还是php的新手我需要如何从这个结构的行中对字符串进行排序,请

A: name pass { date=xx-xx-xxxx ; name=something ; email=xxx@xx.xx }

我只需要从这条线上得到未知的日期xx xx xxxx并减少一天。例如2016年12月29日,下降后将是这样的:2016年12日28日

原始代码:

<?php
$file_handle = fopen("file.txt", "r");
$email = "test@gmail.com"; 

while (!feof($file_handle)) {
   $line = fgets($file_handle);
     if (strpos($line, "$email") !== false)
        {
           $fdate = preg_match('/date=('d{1,2}-'d{1,2}-'d{4})/',$line , $date);
           $adate = date('d-m-Y', (strtotime($date[1]) - 86400));
           $data = explode(" ", $line);
           echo $adate ;
           echo "'n";
        }
}
fclose($file_handle);
?>

这个怎么样:

$string = 'A: name pass { date=29-12-2016, ; name=something ; email=xxx@xx.xx }';
preg_match('/date=('d{1,2}-'d{1,2}-'d{4})/', $string, $date);
echo date('d-m-Y', (strtotime($date[1]) - 86400));

输出:

28-12-2016

这将日期拉到一个变量中,然后减去一天(86400秒)。

Regex101演示:https://regex101.com/r/dK8aN1/1