Php text operations class _ PHP Tutorial

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.