我正在尝试使用PHP删除大型文本文件上的行集。我可以删除行集,但是我不想在其后留空行。请指导我如何使用现有代码进行操作。 My ref question
Example.txt:
MaxBytes[192.168.1.1]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE>
MaxBytes[192.168.1.2]: 30000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.2</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE>
MaxBytes[192.168.1.3]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.3</TD></TR>
<TR><TD>Max Speed:</TD> <TD>200</TD></TR>
</TABLE>
从php中删除行后的Example.txt:
MaxBytes[192.168.1.1]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
<TR><TD>Max Speed:</TD> <TD>300</TD></TR>
</TABLE>
MaxBytes[192.168.1.3]: 10000
<TABLE>
<TR><TD>IP Address:</TD><TD>192.168.1.3</TD></TR>
<TR><TD>Max Speed:</TD> <TD>200</TD></TR>
</TABLE>
代码:
$first = $line_number[0] -1; --> Getting first line of set from external source
$last = $line_number1[0] -1; --> Getting last line of set from external source
$lines = file($dir, FILE_IGNORE_NEW_LINES);
//$out = array();
for ($x = $first; $x <= $last; $x++) {
echo "Line number to be delete : $x <br>";
$lines[$x] = '';
}
//var_dump($lines);
file_put_contents($dir , implode("\n", $lines));
答案 0 :(得分:0)
与其将数组键的值设置为空白,不如将其从数组中删除
如此
$lines[$x] = '';
成为
unset($lines[$x]);