Using PHP to replace a string in a flat file database

There are quite a few different topics on this topic, but I could not fully understand the solution to my problem.

What I would like to do is pretty simple, I have a db with flat files, with data like this -

$username:$worldLocation:$resources 

The problem is that I would like to have a send data html page that would update this line based on a term search using php

 search db for - $worldLocation if $worldLocation found replace entire line with $username:$worldLocation:$updatedResources 

I know that there should be a fairly simple way to do this, but I can’t figure it out at the moment, I will continue to try as this post is up, but if you know a way that I could use it, I would really appreciate the help.

thanks

+4
source share
4 answers

I always liked c and the functions that came in php with c.

Check out fscanf and fprintf .

This will simplify your life when reading letters in a format. For instance:

 $filehandle = fopen("file.txt", "c"); while($values = fscanf($filehandle, "%s\t%s\t%s\n")){ list($a, $b, $c) = $values; // do something with a,b,c } 

In addition, there is no workaround to prevent reading the entire file into memory -> changing one line -> writing the entire file . You have to do it.

It is as effective as you can get. Because you are most likely using native c code, since I am reading some where this php just wraps c functions in these cases.

+1
source
  • You like the hard way, so be it.
  • Make each line the same length. Add a space, tab, capital X, etc., to fill in the gaps.
  • If you want to replace a string, find it, and since each string has a fixed length, you can replace it.
  • For speed and less hassle, use a database (even SQLLite).
0
source

If you are attached to a flat file, the simplest thing is repeated through each line, writing a new file and changing the one that matches.

Yes, that sucks.

I highly recommend switching to the β€œright” database. If you are concerned about the resources or the complexity of the server, you can explore SQLite or Berkeley DB. Both of them use a database, which is "just a file", eliminating the problem of installing and maintaining a database server, but nevertheless you can quickly and easily search, replace and delete individual records. If you still need a flat file for some other reason, you can easily write some import / export procedures.

Another interesting feature if you want to be creative is to look at your file system as a database. Give each user a directory. Each directory has a file for locations. In each file, update the resources. This means that to insert a line, you simply write to a new file. To update a file, you simply rewrite one file. Removing a user is just starting a directory. Undoubtedly, there is a bit more overhead to keep it all in mind.

Other ways to solve the problem may be to make your flat file write-only, since adding to the end of the file is a trivial operation. Then you create a second file that shows the dead line numbers that should be ignored when reading a flat file. In the same way, you can easily β€œX” from existing lines (which again is much easier than trying to update lines in a file that might not be the same length) and add your new data to the end.

These two ideas are not really intended for practical solutions, as they show you that there is always more than one way to solve a problem.

0
source

This is what you are for:

 update `field` from `table` set `field to replace` = '$username:$worldlocation:$updatesResources' where `field` = '$worldLocation'; 
-1
source

Source: https://habr.com/ru/post/1437859/


All Articles