Overwrite Line in File with PHP

后端 未结 7 2051
天命终不由人
天命终不由人 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: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
    

提交回复
热议问题