如何从文本文件中更改一行(PHP)
Text file(text.txt):
#Minecraft server properties
#Tue Sep 23 18:07:26 CEST 2014
generator-settings=
op-permission-level=4
allow-nether=true
level-name=world
enable-query=true
allow-flight=false
announce-player-achievements=true
server-port=25565
query.port=25565
level-type=DEFAULT
enable-rcon=false
force-gamemode=false
level-seed=
server-ip=
max-build-height=256
How to replace the value of some line, such as these:
server-port=25565
Replace with:
server-port=25585
But no to find 'server-port=25565' and replace with 'server-port=25585' It is found that the lines in which the server port and to allocate a value that needs to be replaced.
Example:
<?php
$myfile = fopen("text.txt", "r") or die("Unable to open file!");
...
fclose($myfile);
?>
EDIT: And when finded this and saved replace the text file.
文本文件(text.txt): p>
#Minecraft 服务器属性
#Tue Sep 23 18:07:26 CEST 2014
generator-settings =
op-permission-level = 4
allow-nether = true
level-name = world
enable-query = true
allow-flight =假
announce玩家,成就= TRUE
服务器端口= 25565
query.port = 25565
级型= DEFAULT
enable-RCON =假
Force的游戏模式=假
级种子=
服务器,IP =
max-build-height = 256
code> pre>
如何替换某些行的值,例如: p>
server-port = 25565
code> pre>
替换为: p>
server-port = 25585
代码> pre>
但是没有找到'server-port = 25565'并替换为'server-port = 25585'
发现服务器端口所在的行并分配一个 需要替换的值。 p>
示例: p>
&lt;?php
$ myfile = fopen(“text.txt” ,“r”)或死亡(“无法打开文件!”);
...
fclose($ myfile);
?&gt;
code> pre>
\ n 编辑:
当找到这个并保存时替换文本文件。 p>
div>
Since your format looks suspiciously like a PHP configuration file (.ini)
, why not using the parse_ini_file function?
$ini = parse_ini_file("text.txt");
echo "<pre>".print_r($ini,TRUE)."</pre>";
or
echo $ini["server-port"];
Change it:
$ini["server-port"] = 25585;
Save again your .txt file with:
$f = fopen("text.txt","w");
foreach($ini as $k=>$v) {
fwrite($f,$k."=".$v.PHP_EOL);
}
fclose($f);
You may need to change your #
comments symbol though with ;
UPDATE
$lines = file("text.txt",FILE_IGNORE_NEW_LINES);
// modify
foreach($lines as &$line) {
$val = explode("=",$line);
if ($val[0]=="server-port") {
$val[1] = "25585";
$line = implode("=",$val);
}
}
unset($line);
// save again
$f = fopen("text.txt","w");
foreach($lines as $line) {
fwrite($f,$line.PHP_EOL);
}
fclose($f);
You can use strpos()
or stripos()
<?php
$file = "file.txt";
$content = file($file);
foreach ($content as $line_num => $line) {
if (false === (strpos($line, 'server-port=25565'))) continue;
$content[$line_num] = "server-port=25585
";
}
file_put_contents($file, $content);
Before
#Minecraft server properties #Tue Sep 23 18:07:26 CEST 2014 generator-settings= op-permission-level=4 allow-nether=true level-name=world enable-query=true allow-flight=false announce-player-achievements=true server-port=25565 query.port=25565 level-type=DEFAULT enable-rcon=false force-gamemode=false level-seed= server-ip= max-build-height=256
After
#Minecraft server properties #Tue Sep 23 18:07:26 CEST 2014 generator-settings= op-permission-level=4 allow-nether=true level-name=world enable-query=true allow-flight=false announce-player-achievements=true server-port=25585 query.port=25565 level-type=DEFAULT enable-rcon=false force-gamemode=false level-seed= server-ip= max-build-height=256
Edit
You can modify the $new_port
variable to anything you want.
$path = "file.txt";
$new_port = 25585;
$content = file($path);
foreach ($content as $line_num => $line) {
if (false === (strpos($line, 'server-port'))) continue;
$content[$line_num] = "server-port=$new_port
";
}
file_put_contents($path, $content);