我有一个log.php
,我想substr
一个file_get_contents("application\logs\log.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
答案 0 :(得分:1)
您可以使用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)ERROR\s*-\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)ERROR\s*-\s*/' and '/(^|\R)(.*?)\s*-->\s*/'
和'$1' and '$1$2 '
是相应的替换字符串。在这些字符串中,表达式$1
引用模式中的第一个匹配组(在两种情况下,即(^|\R)
都匹配行的开头或新行)。在第二个替换中,$2
用于输出从行首到第一个-->
的所有字符,即第二组(.*?)
捕获的那些字符。