Text file database processing function library

Source: Internet
Author: User
I can't afford to rent MySQL support, so I can only use flat file storage. I use common perl | bounded format, such as name | email | homepage. I wrote a few functions and thought it was a little useful. What do you have and want to tell me? I want to improve it .? Php // dtf. inc. php3 // DT because I cannot afford MySQL support, I can only use flat file storage,

I use the "|" format commonly used by perl, such as "name | email | homepage.

I wrote a few functions and thought it was a little useful. What do you have,

I also hope to tell you how to perfect it.






// Dtf. inc. php3

//

// DTF -- Delimited Text File Functions

// Written by: Goghs

// Http://www.eqiao.com

//

// Function to manage a delimited text file database



Function dtf_fetch_all_as_string ($ filename ){

$ Fp = fopen ($ filename, "r ");

$ Current = fread ($ fp, filesize ($ filename ));

Fclose ($ fp );

Return $ current;

}





Function dtf_fetch_all_as_array ($ dbname ){

Return file ($ dbname );

}



Function dtf_update_db ($ dbname, $ item ){

If (file_exists ($ dbname )):

$ Fp = fopen ($ dbname, "w + ");

Fputs ($ fp, $ item );

Fclose ($ fp );

Else:

$ Fp = fopen ($ dbname, "w ");

Fputs ($ fp, $ item );

Fclose ($ fp );

Endif;

}



Function dtf_get_total_rows ($ dbname ){

Return count (file ($ dbname ));

}



Function dtf_remove_pipe ($ input ){

If (is_string ($ input )){

// $ Input = ereg_replace ("|", "", $ input); // It's said that str-replace is faster than ereg_replace

$ Input = str_replace ("|", "", $ input );

}

If (is_array ($ input )){

For ($ I = 0; $ I
$ Input [$ I] = ereg_replace ("|", "|", $ input [$ I]);

}

}

Return $ input;

}





// This function is useless. you can use htmlspecialchars.

// However, it is useful if you only need to process "<", ">" and do not need to process quotation marks and &

Function dtf_remove_html_tag ($ input ){

If (is_string ($ input )){

$ Input = ereg_replace ("<", "<", $ input );

$ Input = ereg_replace (">", ">", $ input );

}

If (is_array ($ input )){

For ($ I = 0; $ I
$ Input [$ I] = ereg_replace ("<", "<", $ input [$ I]);

$ Input [$ I] = ereg_replace (">", ">", $ input [$ I]);

}

}

Return $ input;

}





// You can customize this function to determine whether html tags are allowed.

Function dtf_sanitize ($ input ){

$ Input = dtf_remove_pipe ($ input );

// $ Input = dtf_remove_html_tag ($ input );

$ Input = stripslashes ($ input );

$ Input = htmlspecialchars ($ input );

Return $ input;

}



? & G

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.