Substituir string no arquivo de texto usando PHP

Eu preciso abrir um arquivo de texto e substituir uma string. eu preciso disso

Old String: <span id="$msgid" style="display: block;">
New String: <span id="$msgid" style="display: none;">

Isso é o que eu tenho até agora, mas não vejo nenhuma alteração no arquivo de texto além de espaços em branco extras.

$msgid = $_GET['msgid'];

$oldMessage = "";
$deletedFormat = "";

// Read the entire string
$str = implode("\n", file('msghistory.txt'));

$fp = fopen('msghistory.txt', 'w');

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

fwrite($fp, $str, strlen($str));
fclose($fp);

Como eu posso fazer isso?

questionAnswers(3)

yourAnswerToTheQuestion