简体   繁体   English

是否有可能覆盖php中txt文件的特定部分?

[英]Is it possible to overwrite a certain part of a txt file in php?

Let's say we have a .txt file with the text "1234567890", is it possible to change the 1 to a 9 so it says "9234567890" without touching the remaining numbers? 假设我们有一个.txt文件,其文本为“ 1234567890”,是否可以将1更改为9,使其显示为“ 9234567890”,而无需触摸其余数字?

To clarify, it should only change a part of the text file and not rewrite the entire thing. 为了澄清,它应该改变文本文件的一部分,而不是重写整个事情。 I'm only allowed to use php and html. 我只允许使用php和html。

This can be a one-liner, but I'm breaking it up so it makes more sense. 这可能是一个单行,但是我将其分解,所以更有意义。 Further, this would be faster using exec with sed or awk, but this is just PHP here. 此外,将exec与sed或awk一起使用会更快,但这只是PHP。

$string = file_get_contents("yourfile");
$string = str_replace('1','9',$string);
file_put_contents("youfile", $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM