首页 > 解决方案 > PHP查找和删除大文本文件中的特定行集

问题描述

我正在尝试删除基于 ipaddress 的大型文本文件中的某些行集。60,000 行。每行集从 MaxBytes[ipaddress] 开始并以</TABLE>每行集之间的空白行结尾。文本文件中的表格行存在差异。

采样线组:

MaxBytes[192.168.1.1]: 10000  <--start line
 <TABLE>
   <TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
   <TR><TD>Max Speed:</TD> <TD>300</TD></TR>
 </TABLE> <-- end line (Need to delete lines from start to end line)

我正在尝试使用以下代码(由 Yerke 支持)查找起始行,但无法找到查找包含</table>标签的下一行编号的方法。我需要找出包含特定 ipaddress 的行集的开始和结束行号并将其删除。

我是编码初学者,所以我可能需要扩展指导。

代码:

<?php
$dir = "example.txt";
$searchstrt = "192.168.1.1";

///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
    $line_number = [];
    if ($file_handler = fopen($dir, "r")) {
        $i = 0;
        while ($line = fgets($file_handler)) {
            $i++;
            //case sensitive is false by default
            if($case_sensitive == false) {
                $searchstrt = strtolower($searchstrt);
                $line = strtolower($line);
            }
            //find the string and store it in an array
            if(strpos($line, $searchstrt) !== false){
                $line_number[] =  $i;
            }
        }
        fclose($file_handler);
    }else{
        return "File not exists, Please check the file path or dir";
    }

    return $line_number;
}

$line_number = find_line_number_by_string($dir, $searchstrt);
var_dump($line_number);
?>

示例示例.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.1</TD></TR>
   <TR><TD>Max Speed:</TD> <TD>300</TD></TR>
   <TR><TD>Name:</TD> <TD>ABC</TD></TR>
 </TABLE>

MaxBytes[192.168.1.3]: 10000
 <TABLE>
   <TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>
   <TR><TD>Max Speed:</TD> <TD>200</TD></TR>
   <TR><TD>Location:</TD> <TD>INDIA</TD></TR>
 </TABLE>

我找到了一些解决方法来获取包含所需 IP 地址的行集的行号。有没有人建议更好的方法来做到这一点。

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);

$dir = "example.txt";
$searchstrt = "192.168.1.2";
$searchend = "</TABLE>";

///// find details
function find_line_number_by_string($dir, $searchstrt, $case_sensitive=false ) {
    $line_number = [];

    if ($file_handler = fopen($dir, "r")) {
        $i = 0;
        while ($line = fgets($file_handler)) {
            $i++;
            //case sensitive is false by default
            if($case_sensitive == false) {
                $searchstrt = strtolower($searchstrt);
                $line = strtolower($line);
            }
            //find the string and store it in an array
            if(strpos($line, $searchstrt) !== false){
                $line_number[] =  $i;
            }
        }
        fclose($file_handler);
    }else{
        return "File not exists, Please check the file path or dir";
    }

    return $line_number;
}

$line_number = find_line_number_by_string($dir, $searchstrt);
//var_dump($line_number);
$start = $line_number[0];

////////////////////////

function find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start)  {
    $line_number1 = [];
    if ($file_handler1 = fopen($dir, "r")) {
        $i = $start;
//      $i = 0;
        while ($line1 = fgets($file_handler1)) {
            $i++;
            //case sensitive is false by default
            if($case_sensitive == false) {
                $searchend = strtolower($searchend);
                $line1 = strtolower($line1);
            }
            //find the string and store it in an array
            if(strpos($line1, $searchend) !== false){
                $line_number1[] =  $i;
            }
        }

        fclose($file_handler1);
    }else{
        return "File not exists, Please check the file path or dir";
    }

    return $line_number1;
}

$line_number1 = find_line_number_by_string1($dir, $searchend, $case_sensitive=false, $start);
$first = $line_number[0];
$last = $line_number1[0];
//var_dump($line_number1);

for ($x = $first; $x <= $last; $x++) {
    echo "Line number to be delete : $x <br>";
}

?>

标签: phptext

解决方案


我找到了我的问题的解决方案。我刚刚在现有代码中添加了几行。现在它可以根据需要正常工作。

$lines = file($dir, FILE_IGNORE_NEW_LINES);
for ($x = $first; $x <= $last; $x++) {
    echo "Line number to be delete : $x <br>";
    $lines[$x] = '';
     unset($lines[$x]);
}

//var_dump($lines);
file_put_contents($dir , implode("\n", $lines));

推荐阅读