doukuilian8365 2015-05-29 05:11
浏览 42
已采纳

如何在php中的文件中打印匹配的字符串

{<?php

    $out = file_get_contents('a3.log');
    $delimiter = '#';
    $startTag = 'Charge';
    $endTag = 'NAtoms';
    $regex = $delimiter . preg_quote($startTag, $delimiter) 
     . '(.*?)' 
    . preg_quote($endTag, $delimiter) 
    . $delimiter
    . 's';
    preg_match($regex,$out,$matches);
    $match = $matches[0]."<br>";

echo $match."<br>";

    ?> 

}

The file a3.log contain details like

Charge
6                     4.39331  -0.36529   0.08491 
6                     1.60148   0.52177  -0.08236 
6                     4.0146    0.93715  -0.43575 
6                     3.28139  -1.1903    0.52189 
6                     1.9762   -0.77507   0.44518 
6                     2.71      1.35164  -0.51025 
8                     5.59201  -0.74722   0.15251 
8                     0.39622   0.90313  -0.16438 
1                     4.82888   1.57772  -0.77068 
1                     3.53221  -2.17174   0.92114 
1                     1.16487  -1.41898   0.78213 
1                     2.45473   2.33299  -0.90721 
6                    -3.24638  -1.04733  -0.60259 
6                    -2.62068   0.05335  -0.01331 
7                    -4.56997  -1.24972  -0.61786 
6                    -3.43634   1.00395   0.59709 
6                    -5.32808  -0.32334  -0.02513 
NAtoms

And output I expect is just like this.

But output I got after running above code is like

Charge 6 4.39331 -0.36529 0.08491 6 1.60148 0.52177 -0.08236 6 4.0146 
0.93715 -0.43575 6 3.28139 -1.1903 0.52189 6 1.9762 -0.77507 0.44518 6 
2.71 1.35164 -0.51025 8 5.59201 -0.74722 0.15251 8 0.39622 0.90313 
-0.16438 1 4.82888 1.57772 -0.77068 1 3.53221 -2.17174 0.92114 1 1.16487 
-1.41898 0.78213 1 2.45473 2.33299 -0.90721 6 -3.24638 -1.04733 -0.60259 
6 -2.62068 0.05335 -0.01331 7 -4.56997 -1.24972 -0.61786 6 -3.43634 
1.00395 0.59709 6 -5.32808 -0.32334 -0.02513 6 -4.81776 0.81748 0.59417 1 
-2.64561 -1.81055 -1.09513 1 -1.53556 0.17446 -0.03037 1 -2.98949 1.87809 
1.06402 1 -6.4037 -0.50137 -0.04556 1 -5.48881 1.53609 1.05843w NAtoms

how can I get output in new line just shown above

  • 写回答

1条回答 默认 最新

  • dongyuan6949 2015-05-29 05:23
    关注

    To preserve the whitespace and line breaks, wrap your output in <pre></pre> tags. Example:

    echo "<pre>" . $match . "</pre>";
    

    Demo: http://tehplayground.com/#y8yKb8225

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.