需要帮助编写一个php脚本来生成一个文本文件


Need help writing a php script to generate a text file

我不是一个php专家。

我正在一个VOIP项目上工作,并且必须在我的虚拟盒子中打开端口范围(10000 - 20000)。问题是,没有这样的选择可以一次完成所有工作。必须使用命令行逐一完成:

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10001,,10001"
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10002,,10002"
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10003,,10003"

等等,直到到达

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,20000,,20000"

谁能帮我用一个php脚本,将生成一个TXT文件,其中将包含

VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,10001,,10001"
to
VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,20000,,20000"

谢谢

 <?php
  $fp = fopen("outfile.txt", "w");
  for($x = 10000; $x<=20000; $x++){
      fprintf($fp , 'VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh%d,udp,,%d,,%d"'."'n", $x - 9999, $x, $x);
  }
  fclose($fp);

Stackoverflow是用来帮助你解决问题的,而不是为你编码。

,

$output = '';
for ($i = 10001; $i <= 20000; ++$i) {
    $output .= 'VBoxManage modifyvm "Elastix 4.0" --natpf1 "guestssh,udp,,' . $i . ',,' . $i . '"' . PHP_EOL;
}
$f = fopen('commands.txt', 'w');
fwrite($f, $output);
fclose($f);

这是一个简单的解决方案,使用file_put_contents()将您写的文本打印到一个名为"this_is_my_txt_file.txt"的文件中。

file_put_contents('this_is_my_txt_file.txt', "VBoxManage modifyvm '"Elastix 4.0'" --natpf1 '"guestssh,udp,,10001,,10001'" to VBoxManage modifyvm '"Elastix 4.0'" --natpf1 '"guestssh,udp,,20000,,20000'"'n'r");

编辑哦,我现在明白你把问题改了。现在,恐怕我不能理解它。您希望它查看每一行,并在到达"guestssh,udp,,20000,,20000"时停止并将其打印到文件中?

请详细说明你想做什么。

$content = "";
for($i=1;$i<=10000;$i++){
$content.="VBoxManage modifyvm "Elastix 4.0" --natpf1 '"guestssh{$i},udp,,{$i+10000},,{$i+10000}'"'n";
}
file_put_contents('file.txt', $content);