Php text Operation class. I have previously written a simple text Operation class, but I have never had the chance to use it. The text is not as good as the database data is saved in rows and ended with n, note that the data you enter must be a simple text Operation class.
I wrote it before, but I never got a chance to use it. The text is not as good as the database.
Data is saved in rows and ended with \ n. Note that the data you input must end with "\ n", which is the most basic class member, pay special attention to the efficiency of the text. for example, if you want to update 2, 3, and 4 lines of the file, use:
For ($ I = 2; $ I <= 4; $ I ++) $ this-> update ($ file, $ I, $ msg:
$ Data = $ this-> read ($ file );
For ($ I = 2; $ I <= 4; $ I ++) $ data [$ I] = $ msg;
$ This-> write ($ file, $ data );
Class text {
Var $ key =" "; // Data protection
Var $ keylen; // data protection length
Function text () // class constructor
{
$ This-> keylen = strlen ($ this-> lkeylen); // The protected length.
}
Function read ($ file) // read the file, returns an array
{
If (file_exists ($ file )):
$ Data = @ file ($ file );
$ Data [0] = substr ($ data [0], $ this-> keylen );
Return $;
Endif;
}
Function write ($ file, $ data = array () // write a file. The data is an array.
{
If ($ fp = fopen ($ file, "w ")):
Flock ($ fp, 3 );
Fwrite ($ fp, $ this-> key );
Foeach ($ data as $ v) fwrite ($ fp, $ v );
Return true;
Endif;
}
Function add ($ file, $ msg)
// Add data to the end of the file (create a file if it does not exist)
{
$ A = $ this-> read ($ file); // no matter whether the object exists or not
$ A [] = $ msg;
Return $ this-> write ($ file, $ );
}
Function insert ($ file, $ n, $ msg) // insert data before $ n
{
If ($ a = $ this-> read ($ file )):
$ A [$ n] = $ msg. $ a [$ n];
Return true;
Endif;
}
Function updata ($ file, $ n, $ msg) // update $ n data
{
If ($ a = $ this-> read ($ file )):
$ A [$ n] = $ msg;
Return true;
Endif
}
Function del ($ file, $ n) // delete $ n data
{
Return $ this-> updata ($ file, $ n ,"");
}
}
Slave I wrote previously, but I never had the chance to use it. The text is not as good as the database data is saved in rows and ended with \ n. Note that the data you entered is required...