dou448172583 2018-07-02 03:59
浏览 166
已采纳

PHP如何从大文本文件中删除行集而不留空行

I am trying to delete set of lines on large text file using PHP. I am able to remove line set but its leaving blank line behind which I don't want. Kindly guide me how to do this with my existing codes. 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>

Example.txt after removing lines from php :

    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>

Codes :

$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("
", $lines));
  • 写回答

1条回答 默认 最新

  • duan6301 2018-07-02 04:09
    关注

    instead of setting the value of the array key to blank, remove it from the array

    so

    $lines[$x] = '';
    

    becomes

    unset($lines[$x]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!