我有一个log.php
,我想substr
一个file_get_contents("applicationlogslog.php");
函数,
日志文件包含:
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507
如何摆脱'ERROR -'
字符串并删除第一个箭头'->'
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
您可以使用preg_replace
来提供最大的灵活性:
$err = "ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507
ERROR - 2018-09-17 06:51:03 --> Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507";
echo preg_replace(array('/(^|R)ERRORs*-s*/', '/(^|R)(.*?)s*-->s*/'), array('$1', '$1$2 '), $err);
输出:
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'Catatan' /var/www/html/minilos/application/views/minilos/form_akkk.php 483
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'Rekomendasi' /var/www/html/minilos/application/views/minilos/form_akkk.php 502
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507
2018-09-17 06:51:03 Severity: Warning --> Illegal string offset 'name' /var/www/html/minilos/application/views/minilos/form_akkk.php 507
preg_replace调用有两种要匹配的模式:'/(^|R)ERRORs*-s*/' and '/(^|R)(.*?)s*-->s*/'
和'$1' and '$1$2 '
是相应的替换字符串。在这些字符串中,表达式$1
引用模式中的第一个匹配组(在这两种情况下,(^|R)
匹配行首或新行(。在第二个替换中,$2
用于输出从行首到第一个-->
的所有字符,即第二组(.*?)
捕获的字符。