2016-11-05 9 views
1

iが(より多くの行を持つ)次のフォーマットを持つテキストファイルを持っている:印刷、テキストファイルの各行にのみ、特定の単語の後:

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

を試してみてください。それはちょうど ''/'' – Barmar

答えて

0

`preg_quote`へのあなたの2番目の引数が間違っているこの

<?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); 

?> 
関連する問題