find and replace values in a flat-file using PHP

前端 未结 2 826
别跟我提以往
别跟我提以往 2021-01-14 06:27

I\'d think there was a question on this already, but I can\'t find one. Maybe the solution is too easy... Anyway, I have a flat-file and want to let the user change the valu

2条回答
  •  有刺的猬
    2021-01-14 07:30

    A very easy way would it be to use str_replace() on the whole file:

    // read the file
    $file = file_get_contents('filename.csv');
    // replace the data
    $file = str_replace('folder|oldValue', 'folder|newValue', $file);
    // write the file
    file_put_contents('filename.csv', $file);
    

    That should work as only one user the file at a time. But a database is always a better idea. If you need CSV, it's better to have an additional DB to CSV script than only a CSV file.

提交回复
热议问题