使用 PHP 替换文本文件中的字符串 [英] Replace string in text file using PHP

查看:35
本文介绍了使用 PHP 替换文本文件中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to open a text file and replace a string. I need this

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

这是我目前所拥有的,但除了额外的空格之外,我没有看到文本文件中有任何变化.

This is what I have so far, but I don't see any changes in the text file besides extra white spaces.

$msgid = $_GET['msgid'];

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

// Read the entire string
$str = implode("
", 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 替换文本文件中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆