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

时间:2018-05-31 05:13:56

标签: php text lines

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

我是编码的初学者,所以我可能需要更多的指导。

我需要更改的行:

  1. MaxBytes[192.168.1.1]: 10000。这1000可以是任何数字,但IP地址在这里是唯一的。我需要更改它MaxBytes [192.168.1.1]:20000(指定号码);

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

  3. 示例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{}
    
    ?>
    

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:-2)

你的问题和代码中有一些不完全可以理解的东西,当然它可以用更优化的方式写出来 但是考虑到你分享的当前代码,我认为这改变了你需要的变种。

<?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{}

?>

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