Php 从txt中删除一个单词并保存。没有类似的词

Php 从txt中删除一个单词并保存。没有类似的词,php,Php,我有这个功能: <?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 $

我有这个功能:

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

?>
因此,它应该只替换3个,但结果是:

ANAff
b

c
换句话说,我想从log.txt中删除$post值。如何做到这一点?
如果可能的话,如何删除完整的行?不要让它为空。

请尝试以下代码

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

不确定这个问题本身是否好,但这个要求肯定是而且也是有趣的:)很好!有时我也有URL,比如:
http://www.stac
。。。我把这个URL放在了玛丽身上,但它不起作用。可能是因为http:
/
?是的,您必须转义所有特殊字符,如\/No,因为htmlspecialchars用于html呈现。看看
echo preg_replace('/marie\b/', '', $str);