簡體   English   中英

PHP查找和刪除大文本文件中的特定行集

[英]PHP Find and delete specific line set in large text file

我正在嘗試刪除基於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);
?>

范例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.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>";
}

?>

我找到了解決問題的辦法。 我剛剛在現有代碼中添加了幾行。 現在其工作正常。

$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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM