PHP text Operation class instance code

Source: Internet
Author: User
This article mainly introduces the PHP implementation of the file operation class and file download function, combined with an example of PHP for file read, write, create and download the functions of the implementation skills, the need for friends can refer to the next

<?php class Ctbclass {var $file;               var $index; Create a file and write to input function Null_write ($new) {$f =fopen ($this->file, "                                   W ");                           Flock ($f, LOCK_EX);                                   Fputs ($f, $new);                 Fclose ($f); }//Add data record to file End Function Add_write ($new) {$f =fopen ($t                                   His->file, "a");                           Flock ($f, LOCK_EX);                                     Fputs ($f, $new);                 Fclose ($f);                           }//In conjunction with the return of ReadFile (), convert a row of data to a one-dimensional array function Make_array ($line) {                           $array = Explode ("\x0e", $line);               return $array;                      }//Convert one-dimensional array to a single row of data function Join_array ($line) {     $array = Join ("\x0e", $line);               return $array;                                   }//Returns the total number of data files in function Getlines () {$f =file ($this->file);                       return count ($f); }//Returns the next row of data records (fallback) function Next_line () {$this->index= $this-                                   >index++;                       return $this->get (); }//Return data record on previous row (standby) function Prev_line () {$this->index= $this-                                   >index--;                       return $this->get (); }//Returns data record data for the current row smaller function get () {$f =fopen ($this->file,                                   "R");                           Flock ($f, lock_sh);                                  for ($i =0; $i <= $this->index; $i +) {     $rec =fgets ($f, 1024);                           } $line =explode ("\x0e", $rec);                           Fclose ($f);                       return $line; }//Returns data record data for the current row larger 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 =explode ("\x0e", $rec);                           Fclose ($f);                       return $line; }//Open Data file---Returns the file contents as a one-dimensional array function read_file () {if (file_ex                           Ists ($this->file)) {$line =file ($this->file); } return $line; }//Open Data file---Returns the file contents in a 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;   }//Pass in an array, merge into one row of data, rewrite 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 row of data records 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 row of data records to the front of the file function Insert_line ($array) {$NEWFI                           Le = Implode ("\x0e", $array);                           $f = fopen ($this->file, "R");                           Flock ($f, lock_sh);                           while ($line = fgets ($f, 1024x768)) {$newfile. = $line;                           } fclose ($f);                           $f = fopen ($this->file, "w"); Flock ($f, LOCK_EX);                          Fputs ($f, $newfile);               Fclose ($f);                           }//Update all eligible data records for a larger case of byte data per row 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 <count ($FC), $i + +) {$list = explode ("\x0e", $FC [$i]); if ($list [$column]! = $query _string) {$n Ewfile = $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 qualifying data records for small byte data per row 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, 1024x768)) {$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 qualifying data records for large byte data per row function Delete ($column, $query _string) {                                       $newfile = "";                           $FC =file ($this->file);                           $f =fopen ($this->file, "R");                           Flock ($f, lock_sh);                                       for ($i =0; $i <count ($FC), $i + +) {$list = explode ("\x0e", $FC [$i]); if ($list [$column]! = $query _string) {$n Ewfile = $newfile. Chop ($FC [$i]). "                                       \ n ";          }                 } fclose ($f);                           $f =fopen ($this->file, "w");                           Flock ($f, LOCK_EX);                           Fputs ($f, $newfile);               Fclose ($f);                               }//Delete all qualifying data records for small byte data per row function Delete2 ($column, $query _string) {                           $newfile = "";                           $f = fopen ($this->file, "R");                           Flock ($f, lock_sh);                                       while ($line = fgets ($f, 1024x768)) {$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); }//Gets 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 =explode ("\x0e", $tlines [$i]);                           $get _value[]= $line [$column];                           } $get _max_value = max ($get _value);               return $get _max_value; }//Based on whether a field in the data file contains $query_string, a two-dimensional array is returned with all eligible data 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;                           }//Functions like function Select (), speed may be slightly elevated function select2 ($column, $query _string) { if (file_exists ($this->file)) {$tline = $this->read_file                                       (); foreach ($tline as $tmpLine) {$line = $this->make_array ($tmpLi                                                   NE); if ($line [$column] = = $query _string) {$lines []= $tmpLine                                                   ;               }}} return $lines; }//Depending on whether a field in the data file contains $query_String to query, returning the first qualifying data function Select_line ($column, $query _string) {$tl in a one-dimensional array                           ine = $this->read_file ();                                       foreach ($tline as $tmpLine) {$line = $this->make_array ($tmpLine); if ($line [$column] = = $query _string) {retur                                                   n $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 {$l                                                                           INE_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 = $line _key + 1;                                                               Break                                                   }}} or else {                                       return 0;          }} $down _line = $this->make_array ($tline [$line _key_down]);                 return $down _line; }}?>

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.