使用PHP替换文本文件中的字符串

我需要打开一个文本文件并替换一个字符串。我需要这个

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

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

到目前为止,这是我所拥有的,但是除了多余的空格外,我看不到文本文件中的任何更改。

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

我该怎么做?

回答:

这项工作:

$msgid = $_GET['msgid'];

$oldMessage = '';

$deletedFormat = '';

//read the entire string

$str=file_get_contents('msghistory.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('msghistory.txt', $str);

以上是 使用PHP替换文本文件中的字符串 的全部内容, 来源链接: utcz.com/qa/428382.html

回到顶部