A class that is working in a text file in PHP

zhaozj2021-02-16  58

Class CTBClass {

Var $ file;

Var $ index;

// Establish a file and write input

Function NULL_WRITE ($ NEW)

{

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ New);

fclose ($ f);

}

// Add data record to the end of the file

Function add_write ($ new) {

$ f = fopen ($ this-> file, "a");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ New);

fclose ($ f);

}

/ / Take the return of readfile () to convert a row of data to a one-dimensional array

Function make_Array ($ line) {

$ array = expedition ("/ x0e", $ line);

Return $ array;

}

/ / Convert a row of data for a one-dimensional array

Function Join_Array ($ LINE) {

$ array = join ("/ x0e", $ line);

Return $ array;

}

/ / Return to the total number of data files

Function getLines () {

$ f = file ($ this-> file);

Return Count ($ F);

}

/ / Return to the data record (standby)

Function next_Line () {

$ this-> index = $ tris-> index ;

Return $ this-> get ();

}

/ / Return to the data record (standby)

Function prev_Line () {

$ this-> index = $ this-> index -;

Return $ this-> get ();

}

/ / Return to the current line of data record data smaller

Function get () {

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

For ($ I = 0; $ I <= $ this-> index; $ i ) {

$ REC = FGETS ($ F, 1024);

}

$ line = evLode ("/ x0e", $ REC);

fclose ($ f);

Return $ LINE;

}

/ / Returns the data record data of the current line

Function GET_BIG_FILE () {

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

For ($ I = 0; $ I <= $ this-> index; $ i ) {

$ REC = fgets ($ F, 1024 * 5);

}

$ line = evLode ("/ x0e", $ REC);

fclose ($ f);

Return $ LINE;

}

// Open data file --- Return to file content in one-dimensional array

Function read_file () {

IF (file_exists ($ this-> file) {

$ line = file ($ this-> file);

}

Return $ LINE;

}

/ / Open data file --- Return to file in two-dimensional array

Function OpenFile () {

IF (file_exists ($ this-> file) {

$ f = file ($ this-> file);

$ LINES = array ();

Foreach ($ f AS $ RAWLINE) {$ TMPLINE = EXPLODE ("/ x0e", $ rawline);

Array_Push ($ LINES, $ TMPLINE);

}

}

Return $ LINES;

}

/ / Incontinent into an array, combine into a line of data, and rewrote the entire file

Function Overwrite ($ array) {

$ newline = iMPLode ("/ x0e", $ array);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ Newline);

fclose ($ f);

}

/ / Add a line of data to the end of the file

Function Add_Line ($ Array, $ Check_n = 1) {

$ S = IMPLODE ("/ x0e", $ array);

$ f = fopen ($ this-> file, "a");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ S);

IF ($ Check_n == 1) FPUTS ($ F, "/ N");

fclose ($ f);

}

// Insert a line of data records to the front of the file

Function Insert_Line ($ array) {

$ newfile = iMPLode ("/ x0e", $ array);

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

While ($ LINE = FGETS ($ F, 1024)) {

$ NEWFILE. = $ LINE;

}

fclose ($ f);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ newfile);

fclose ($ f);

}

/ / Update all of the requirements of the requirements for a larger condition for each line byte data

Function Update ($ column, $ query_string, $ update_array) {

$ update_string = iMPLode ("/ x0e", $ update_array);

$ newfile = "";

$ fc = file ($ this-> file);

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

For ($ I = 0; $ i

$ list = expedition ("/ x0e", $ fc [$ i]);

IF ($ LIST [$ Column]! = $ query_string) {

$ newfile = $ newfile.chop ($ FC [$ I]). "/ n";

} Else {

$ NEWFILE = $ newfile. $ update_string;

}

}

fclose ($ f);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ newfile);

fclose ($ f);

}

/ / Update all the requirements of data records, suitable for situations of each line byte data

Function Update2 ($ column, $ query_string, $ update_array) {

$ NEWLINE = Implode ("/ x0e", $ update_array); $ newfile = "";

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

While ($ LINE = FGETS ($ F, 1024)) {

$ TMPLINE = EXPLODE ("/ x0e", $ line);

IF ($ TMPLINE [$ column] == $ query_string) {

$ NEWFILE. = $ NEWLINE;

} Else {

$ NEWFILE. = $ LINE;

}

}

fclose ($ f);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ newfile);

fclose ($ f);

}

/ / Delete all qualified data records for a larger condition for each line byte data

Function delete ($ column, $ query_string) {

$ newfile = "";

$ fc = file ($ this-> file);

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

For ($ I = 0; $ i

$ list = expedition ("/ x0e", $ fc [$ i]);

IF ($ LIST [$ Column]! = $ query_string) {

$ newfile = $ newfile.chop ($ FC [$ I]). "/ n";

}

}

fclose ($ f);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ newfile);

fclose ($ f);

}

/ / Delete all qualified data records for a smaller condition per line byte data

Function delete2 ($ column, $ query_string) {

$ newfile = "";

$ f = fopen ($ THIS-> File, "R");

FLOCK ($ f, lock_sh);

While ($ LINE = FGETS ($ F, 1024)) {

$ TMPLINE = EXPLODE ("/ x0e", $ line);

IF ($ TMPLINE [$ Column]! = $ query_string) {

$ NEWFILE. = $ LINE;

}

}

fclose ($ f);

$ f = fopen ($ this-> file, "w");

FLOCK ($ f, lock_ex);

FPUTS ($ F, $ newfile);

fclose ($ f);

}

/ / Get the maximum value of a field in a file

Function GET_MAX_VALUE ($ column) {

$ TLINES = File ($ this-> file);

For ($ I = 0; $ i <= count ($ tlines); $ i ) {

$ line = expedition ("/ x0e", $ tlines [$ i]);

$ get_value [] = $ line [$ column];

}

$ get_max_value = max ($ get_value); return $ get_max_value;

}

/ / Returns all eligible data in a two-dimensional array based on a field of the data file contains $ query_string

Function SELECT ($ column, $ query_string) {

$ TLINE = $ this-> openfile ();

$ LINES = array ();

Foreach ($ TLINE AS $ LINE) {

IF ($ LINE [$ column] == $ query_string) {

Array_Push ($ LINES, $ LINE);

}

}

Return $ LINES;

}

// Function like Function Select (), speed may increase slightly

Function SELECT2 ($ column, $ query_string) {

IF (file_exists ($ this-> file) {

$ TLINE = $ this-> read_file ();

Foreach ($ TLINE AS $ TMPLINE) {

$ line = $ this-> make_Array ($ tmpline);

IF ($ LINE [$ column] == $ query_string) {

$ LINES [] = $ tmpline;

}

}

}

Return $ LINES;

}

/ / Returns the first eligible data in one-dimensional array based on a field of the data file contains $ query_string.

Function Select_Line ($ column, $ query_string) {

$ TLINE = $ this-> read_file ();

Foreach ($ TLINE AS $ TMPLINE) {

$ line = $ this-> make_Array ($ tmpline);

IF ($ LINE [$ column] == $ query_string) {

Return $ LINE;

Break;

}

}

}

// select next / prev line (next_prev ==> 1 / next, 2 / prev) by CX

Function select_next_prev_line ($ column, $ query_string, $ next_prev) {

$ TLINE = $ this-> read_file ();

$ line_key_end = count ($ tline) - 1;

$ line_key = -1;

Foreach ($ TLINE AS $ TMPLINE) {

$ LINE_KEY ;

$ line = $ this-> make_Array ($ tmpline);

IF ($ NEXT_PREV == 1) {// next?

IF ($ LINE [$ column] == $ query_string) {

IF ($ LINE_KEY == 0) {

Return 0;

} Else {

$ line_key_up = $ line_key - 1;

Return $ up_LINE;

}

} Else {

$ UP_LINE = $ LINE;

}

} Elseif ($ next_prev == 2) {// prev?

IF ($ LINE [$ column] == $ query_string) {if ($ line_key == $ line_key_end) {

Return 0;

} Else {

$ line_key_down = $ tine_key 1;

Break;

}

}

} Else {

Return 0;

}

}

$ down_line = $ this-> make_Array ($ tline [$ line_key_down]);

Return $ down_LINE;

}

}

转载请注明原文地址:https://www.9cbs.com/read-19190.html

New Post(0)