使用 PHP 在大型文本文件中查找字符串并替换多个值

Find string and replace multiple values in a large text file using PHP

我正在尝试替换大文本文件中的某些值,大约有60,000 行。第一次代码执行正常并替换值,但下次它会跳过行号并在文件末尾附加新值。

我是编码初学者,因此我可能需要更多指导。

我需要更改的行:

  1. MaxBytes[192.168.1.1]: 10000。这个 1000 可以是任何数字,但 IP 地址在这里是唯一的。我需要改一下 MaxBytes[192.168.1.1]: 20000 (Assigned number);

  2. <TR><TD>Max Speed:</TD> <TD>200</TD></TR> 这个200可以是任意数字。我正在跟踪 <TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR> 行并替换下一行,因为 IP 地址是唯一的 here.I 需要将其更改为 <TR><TD>Max Speed:</TD> <TD>500</TD></TR>

样本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>
 </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>
 </TABLE>

这是我使用的代码

<?php
$dir = "Example.txt";
$search = "<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>";
$replacement = "   <TR><TD>Max Speed:</TD> <TD>500</TD></TR>";
$maxbytes = "MaxBytes[192.168.1.1]: ";
$new_line = "MaxBytes[192.168.1.1]: \r";
$newmaxbytes = "MaxBytes[192.168.1.1]: 20000";


///// Change Max Speed
function find_line_number_by_string($dir, $search, $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) {    
                $search = strtolower($search); 
                $line = strtolower($line);      
              }
              //find the string and store it in an array
              if(strpos($line, $search) !== false){
                $line_number .=  $i.",";
              }
           }
           fclose($file_handler);
        }else{
            return "File not exists, Please check the file path or dir";
        }
        //if no match found
        if(count($line_number)){
            return substr($line_number, 0, -1);
        }else{
            return "No match found";
        }
    }

$line_number = find_line_number_by_string($dir, $search);
echo $line_number;

$lines = file($dir, FILE_IGNORE_NEW_LINES);
$lines[$line_number] = $replacement;
file_put_contents($dir , implode("\n", $lines));

///// Change MaxBytes

$contents = file_get_contents($dir);
$new_contents= "";
if( strpos($contents, $maxbytes) !== false) { // if file contains ID
    $contents_array = preg_split("/\r\n|\r|\n/", $contents);
    foreach ($contents_array as &$record) {    // for each line
        if (strpos($record, $maxbytes) !== false) { // if we have found the correct line
            $new_contents .= $new_line; // change record to new record
        }else{
            $new_contents .= $record . "\r";
        }
    }

file_put_contents($dir, $new_contents, LOCK_EX);

$fhandle = fopen($dir,"r");
$content = fread($fhandle,filesize($dir));
$content = str_replace($maxbytes, $newmaxbytes, $content);
$fhandle = fopen($dir,"w");
fwrite($fhandle,$content);
fclose($fhandle);

}else{}

?>

那是因为你删除了所有新行,但没有添加新行,只是回车 returns。

通过在第 58 行插入以下内容来修复:

$new_contents .= $record . "\n";

回车 return (\r) 跳到下一行的下一行,但不会结束当前行。您需要一个换行符 (\n)。在 运行 第二次输入您的代码后,PHP 认为文本文件只是一行文本。

你的问题和代码有些看不懂,当然可以写成更优化的方式。
但是考虑到您共享的当前代码,我想这改变了您需要的变体。

<?php
$dir = "Example.txt";
$search = "<TR><TD>IP Address:</TD><TD>192.168.1.1</TD></TR>";
$replacement = "   <TR><TD>Max Speed:</TD> <TD>10000</TD></TR>";
$maxbytes = "MaxBytes[192.168.1.1]: ";
$new_line = "MaxBytes[192.168.1.1]: \r";
$newmaxbytes = "MaxBytes[192.168.1.1]: 20000";


///// Change Max Speed
function find_line_number_by_string($dir, $search, $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) {
                $search = strtolower($search);
                $line = strtolower($line);
            }
            //find the string and store it in an array
            if(strpos($line, $search) !== 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, $search);
var_dump($line_number);


$lines = file($dir, FILE_IGNORE_NEW_LINES);
if(!empty($line_number)) {
    $lines[$line_number[0]] = $replacement;
}

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

///// Change MaxBytes

$contents = file_get_contents($dir);
$new_contents= "";
if( strpos($contents, $maxbytes) !== false) { // if file contains ID
    $contents_array = preg_split("/\r\n|\r|\n/", $contents);
    foreach ($contents_array as &$record) {    // for each line
        if (strpos($record, $maxbytes) !== false) { // if we have found the correct line
            $new_contents .= $new_line; // change record to new record
        }else{
            $new_contents .= $record . "\n";
        }
    }

    file_put_contents($dir, $new_contents, LOCK_EX);

    $fhandle = fopen($dir,"r");
    $content = fread($fhandle,filesize($dir));
    $content = str_replace($maxbytes, $newmaxbytes, $content);
    $fhandle = fopen($dir,"w");
    fwrite($fhandle,$content);
    fclose($fhandle);

}else{}

?>

如果它不能按您需要的方式工作,请与我联系,我会更新答案..