简体   繁体   English

使用PHP查找和替换平面文件中的值

[英]find and replace values in a flat-file using PHP

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 values based on a name. 也许解决方案太容易了......无论如何,我有一个平面文件,并希望让用户根据名称更改值。 I've already sorted out creating new name+value-pairs using the fopen('a') mode, using jQuery to send the AJAX call with newValue and newName . 我已经整理出了使用fopen('a')模式创建新名称+值对,使用jQuery使用newValuenewName发送AJAX调用。 But say the content looks like this: 但是说内容看起来像这样:

host|http:www.stackoverflow.com
folder|/questions/
folder2|/users/

And now I want to change the folder value. 现在我想更改folder值。 So I'll send in folder as oldName and /tags/ as newValue . 所以我会将folder作为oldName发送,将/tags/作为newValue What's the best way to overwrite the value? 覆盖价值的最佳方法是什么? The order in the list doesn't matter, and the name will always be on the left, followed by a | 列表中的顺序无关紧要,名称将始终位于左侧,后跟| (pipe), the value and then a new-line . (管道),值然后new-line

My first thought was to read the list, store it in an array, search all the [0] 's for oldName , then change the [1] that belongs to it, and then write it back to a file. 我的第一个想法是读取列表,将其存储在一个数组中,搜索所有[0]oldName ,然后更改属于它的[1] ,然后将其写回文件。 But I feel there is a better way around this? 但我觉得有一个更好的解决方法吗? Any ideas? 有任何想法吗? Maybe regex ? 也许正则表达式

A very easy way would it be to use str_replace() on the whole file: 一个非常简单的方法是在整个文件上使用str_replace():

// 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. 如果您需要CSV,最好还有一个额外的数据库到CSV脚本,而不仅仅是一个CSV文件。

You can keep the list in memory using a PHP feature like APC and write it out every so often to disk. 您可以使用像APC这样的PHP功能将列表保留在内存中,并且每隔一段时间就将其写入磁盘。 That way you're only reading it in when it doesn't exist in memory. 这样你就只能在内存中不存在的时候读它。

If you want to be performance orientated storing this info in a CSV is likely not the best to begin with. 如果您希望以性能为导向,以CSV格式存储此信息可能不是最好的开始。 If you don't want to use a true RDMS (MySQL) then I'd suggest using SQLite. 如果您不想使用真正的RDMS(MySQL),那么我建议使用SQLite。 It provides all the features you're looking for (UPDATE) in this case and it acts exactly the same as a CSV in the sense that it's just a file on your harddrive and doesn't have concurrency. 在这种情况下,它提供了您正在寻找的所有功能(更新),它的行为与CSV完全相同,因为它只是硬盘上的文件并且没有并发性。

If that's not an option the other way is to use shell level commands like sed & awk to do inline regular expression changes. 如果这不是一个选项,另一种方法是使用sed和awk等shell级命令来进行内联正则表达式更改。 Lastly, reading the whole file into a string, performing a nasty regex adjustment on it ( s/// ) and then writing it out again would work 最后,将整个文件读成一个字符串,对它执行令人讨厌的正则表达式调整( s/// ),然后再将其写出来就可行了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM