Overwrite Line in File with PHP

后端 未结 7 2005
天命终不由人
天命终不由人 2020-11-30 12:37

What is the best way to overwrite a specific line in a file? I basically want to search a file for the string \'@parsethis\' and overwrite the rest of that line with somethi

相关标签:
7条回答
  • 2020-11-30 12:45

    or if your file isn't too big:

    $sample = file_get_contents('sample');
    $parsed =preg_replace('#@parsethis.*#', 'REPLACE TO END OF LINE', $sample);
    

    You'll have to choose delimiters '#' that aren't present in the file though.

    0 讨论(0)
  • 2020-11-30 12:50

    If the file isn't too big, the best way would probably be to read the file into an array of lines with file(), search through the array of lines for your string and edit that line, then implode() the array back together and fwrite() it back to the file.

    0 讨论(0)
  • 2020-11-30 12:55

    If the file is really big (log files or something like this) and you are willing to sacrifice speed for memory consumption you could open two files and essentially do the trick Jeremy Ruten proposed by using files instead of system memory.

    $source='in.txt';
    $target='out.txt';
    
    // copy operation
    $sh=fopen($source, 'r');
    $th=fopen($target, 'w');
    while (!feof($sh)) {
        $line=fgets($sh);
        if (strpos($line, '@parsethis')!==false) {
            $line='new line to be inserted' . PHP_EOL;
        }
        fwrite($th, $line);
    }
    
    fclose($sh);
    fclose($th);
    
    // delete old source file
    unlink($source);
    // rename target file to source file
    rename($target, $source);
    
    0 讨论(0)
  • 2020-11-30 12:55

    I'd most likely do what Jeremy suggested, but just for an alternate way to do it here is another solution. This has not been tested or used and is for *nix systems.

    $cmd = "grep '@parsethis' " . $filename;
    $output = system($cmd, $result);
    $lines = explode("\n", $result);
    // Read the entire file as a string
    // Do a str_repalce for each item in $lines with ""
    
    0 讨论(0)
  • 2020-11-30 12:59

    This is a solution that works for rewriting only one line of a file in place with sed from PHP. My file contains only style vars and is formatted: $styleVarName: styleVarProperty;\n
    For this I first add the ":" to the ends of myStyleVarName, and sed replaces the rest of that line with the new property and adds a semicolon. Make sure characters are properly escaped in myStyleVarProp.

    $command = "pathToShellScript folder1Name folder2Name myStyleVarName myStyleVarProp";
    shell_exec($command);
    
    /* shellScript */
    #!/bin/bash
    file=/var/www/vhosts/mydomain.com/$1/$2/scss/_variables.scss
    str=$3"$4"
    sed -i "s/^$3.*/$str;/" $file
    
    0 讨论(0)
  • 2020-11-30 13:06

    If you want to completely replace the contents of one file with the contents of another file you can use this:

    rename("./some_path/data.txt", "./some_path/data_backup.txt");
    rename("./some_path/new_data.txt", "./some_path/data.txt");
    

    So in the first line you backup the file and in the second line you replace the file with the contents of a new file.

    As far as I can tell the rename returns a boolean. True if the rename is successful and false if it fails. One could, therefore, only run the second step if the first step is successful to prevent overwriting the file unless a backup has been made successfully. Check out:

    https://www.php.net/manual/en/function.rename.php

    Hope that is useful to someone.

    Cheers

    Adrian

    0 讨论(0)
提交回复
热议问题