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

标签 php file text replace

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

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 - 使用PHP替换文本文件中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11901521/

相关文章:

php - Apache 与 IIS PHP 性能比较

c - ubuntu 使用可加载模块读取文件

iphone - 如何使用\t [tab] 运算符在列中格式化 NSString?

jquery - 使用 jQuery 将数字更改为单词

php - Slim Framework -> 创建 XML 输出

php - 一种使 md5_file() 更快的方法?

php - 为什么 mysqli_data_seek 不工作?

php - 如何使用 php 将选定复选框中的值保存到文件中?

c - 我正在尝试使用 C 编码从 linux 制作 uniq 命令

Delphi:如何将互联网上的文本文件加载为字符串?