str_replace, если в параметре $search указать многострочный текст, не работает.
Извратился вот так:
$text .= "\n'" . $md5 . "':'" . addslashes($text) . "',";
$input = preg_quote($text);
$input = preg_replace("~\n~", '\s*?', $input);
$input = preg_replace("~ ~", '\s*?', $input);
$pattern = "~" . $input . "~uis";
$content = preg_replace($pattern, '', $content);
Но при каком-то $text страница вообще исчезает.