我有一个具有以下格式的文本文件(还有更多行):
00:06:57 : Global: Andy: how is everyone today?
00:06:58 : Global: Ryan: pretty tired
00:07:00 : Global: Ben: meh the usual
00:07:01 : Global: Jim: average
在没有时间或全局的情况下打印或回显每一行的最佳方法是什么:
Andy: how is everyone today?
Ryan: pretty tired
Ben: meh the usual
Jim: average
我试过
<?php
$file = 'chat.txt';
$searchfor = 'Global:';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '(.*)/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern(.*)\$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
echo "Found matches:\n";
echo implode("\n", $matches[0]);
}
else{
echo "No matches found";
}
?>
但是它是空的,它应该被格式化为数组还是json以便更好地处理?谢谢你的时间
答案 0 :(得分:0)
试试这个
<?php
$file = 'chat.txt';
// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');
// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
$pattern = "/..:..:.. : Global: /";
$replacement ="";
echo preg_replace($pattern, $replacement, $contents );
?>