2016-08-27 3 views
1

私はこの機能を持っている:単語をtxtから削除して保存します。全く同様の言葉

<?php 

$post="marie" . "\n"; // \n not working? 

//replace txt 
$oldMessage = $post; 
$deletedFormat = ""; 

//read the entire string 
$str=file_get_contents('log.txt'); 

//replace something in the file string - this is a VERY simple example 
$str=str_replace("$oldMessage", "$deletedFormat",$str); 

//write the entire string 
file_put_contents('log.txt', $str); 

?> 

私はmarieを見つけ、それを交換したい:

ANAff 
marieb 
marie 
mariec 
marie 

だから、わずか3 1を置き換えるべきではありませんが、結果はでした:

ANAff 
b 

c 

つまり、正確に$ post値をlog.txtから削除したいと考えています。これを行う方法? 可能であれば、フルラインを削除する方法は?それを空白にしないでください。

+1

ないその質問が良いそのものですが、その要件は確かにもあると面白いです:) –

答えて

1

は、次のコード

echo preg_replace('/marie\b/', '', $str); 
+0

素敵な場合は必ずしてみてください!時には私もurlもあります:http:// www.stac' ... marieの代わりに私はこのURLを書いていて動作しません。おそらくhttp: '//'のためでしょうか? –

+1

はい、あなたは\/ –

+0

ような特別な文字をエスケープする必要があります! htmlspecialcharsは良い選択ですか? –

関連する問題