PHP classes for text file operations

WBOY
Release: 2016-07-25 08:43:41
Original
885 people have browsed it
  1. var $file;
  2. var $index;
  3. //Create a file and write input
  4. function null_write($new) {
  5. $f=fopen($this-> file,"w");
  6. flock($f,LOCK_EX);
  7. fputs($f,$new);
  8. fclose($f);
  9. }
  10. // Add data records to the end of the file
  11. function add_write($new ) {
  12. $f=fopen($this->file,"a");
  13. flock($f,LOCK_EX);
  14. fputs($f,$new);
  15. fclose($f);
  16. }
  17. / / Used with the return of readfile() to convert a row of data into a one-dimensional array
  18. function make_array($line) {
  19. $array = explode("\x0E",$line);
  20. return $array;
  21. }
  22. / /Convert one row of data into a one-dimensional array
  23. function join_array($line) {
  24. $array = join("\x0E",$line); return $array;
  25. }
  26. // Return the total number of lines in the data file
  27. function getlines () {
  28. $f=file($this->file);
  29. return count($f);
  30. }
  31. // Return the data record of the next line (backup)
  32. function next_line() {
  33. $this-> ;index=$this->index++;
  34. return $this->get();
  35. }
  36. // Return the data record of the previous line (backup)
  37. function prev_line() {
  38. $this->index=$ this->index--;
  39. return $this->get();
  40. }
  41. // Return the data record of the current row. The data is small
  42. function get() {
  43. $f=fopen($this-> file,"r");
  44. flock($f,LOCK_SH);
  45. for($i=0;$i<=$this->index;$i++) {
  46. $rec=fgets($f,1024) ;
  47. }
  48. $line=explode("\x0E",$rec);
  49. fclose($f);
  50. return $line;
  51. }
  52. // Return the data record of the current line. The data is larger
  53. function get_big_file() {
  54. $f=fopen($this->file,"r");
  55. flock($f,LOCK_SH);
  56. for($i=0;$i<=$this->index;$i++) {
  57. $rec=fgets($f,1024*5);
  58. }
  59. $line=explode("\x0E",$rec);
  60. fclose($f);
  61. return $line;
  62. }
  63. // Open data File --- Return the file content as a one-dimensional array
  64. function read_file() {
  65. if (file_exists($this->file)) {
  66. $line =file($this->file);
  67. }
  68. return $ line;
  69. }
  70. // Open the data file --- return the file content as a two-dimensional array
  71. function openFile() {
  72. if (file_exists($this->file)) {
  73. $f =file($this-> ;file);
  74. $lines = array();
  75. foreach ($f as $rawline) {
  76. $tmpline = explode("\x0E",$rawline);
  77. array_push($lines, $tmpline);
  78. }
  79. }
  80. return $lines;
  81. }
  82. // Pass in an array, merge it into one line of data, and rewrite the entire file
  83. function overwrite($array){
  84. $newline = implode("\x0E",$array);
  85. $ f = fopen($this->file,"w");
  86. flock($f,LOCK_EX);
  87. fputs($f,$newline);
  88. fclose($f);
  89. }
  90. // Add a row of data Record to the end of the file
  91. function add_line($array,$check_n=1) {
  92. $s=implode("\x0E",$array);
  93. $f=fopen($this->file,"a");
  94. flock($f,LOCK_EX);
  95. fputs($f,$s);
  96. if ($check_n==1)
  97. fputs($f,"\n");
  98. fclose($f);
  99. }
  100. // Insert a row of data records to the front of the file
  101. function insert_line($array) {
  102. $newfile = implode("\x0E",$array);
  103. $f = fopen($this->file,"r") ;
  104. flock($f,LOCK_SH);
  105. while ($line = fgets($f,1024)) {
  106. $newfile .= $line;
  107. }
  108. fclose($f);
  109. $f = fopen($this ->file,"w");
  110. flock($f,LOCK_EX);
  111. fputs($f,$newfile);
  112. fclose($f);
  113. }
  114. // Update all qualified data records, suitable for situations where the byte data in each row is large
  115. function update($column,$query_string,$update_array) {
  116. $update_string = implode("\x0E",$update_array) ;
  117. $newfile = "";
  118. $fc=file($this->file);
  119. $f=fopen($this->file,"r");
  120. flock($f,LOCK_SH);
  121. for ($i=0;$i $list = explode("\x0E",$fc[$i]);
  122. if ($list[$column] != $query_string) {
  123. $newfile = $newfile.chop($fc[$i])."\n";
  124. } else {
  125. $newfile = $newfile.$update_string;
  126. }
  127. }
  128. fclose($f) ;
  129. $f=fopen($this->file,"w");
  130. flock($f,LOCK_EX);
  131. fputs($f,$newfile);
  132. fclose($f);
  133. }
  134. // Update all qualified data records, suitable for cases where the byte data in each row is small
  135. function update2($column,$query_string,$update_array) {
  136. $newline = implode("\x0E",$update_array);
  137. $ newfile = "";
  138. $f = fopen($this->file,"r");
  139. flock($f,LOCK_SH);
  140. while ($line = fgets($f,1024)) {
  141. $tmpLine = explode("\x0E",$line);
  142. if ($tmpLine[$column] == $query_string) {
  143. $newfile .= $newline;
  144. } else {
  145. $newfile .= $line;
  146. }
  147. }
  148. fclose($f);
  149. $f = fopen($this->file,"w");
  150. flock($f,LOCK_EX);
  151. fputs($f,$newfile);
  152. fclose($f ; $this->file);
  153. $f=fopen($this->file,"r");
  154. flock($f,LOCK_SH);
  155. for ($i=0;$i $list = explode("\x0E",$fc[$i]);
  156. if ($list[$column] != $query_string) {
  157. $newfile = $newfile.chop($ fc[$i])."\n";
  158. }
  159. }
  160. fclose($f);
  161. $f=fopen($this->file,"w");
  162. flock($f,LOCK_EX);
  163. fputs($f,$newfile);
  164. fclose($f);
  165. }
  166. // Delete all qualified data records, suitable for cases where the byte data per row is small
  167. function delete2($column,$query_string ){
  168. $newfile = "";
  169. $f = fopen($this->file,"r");
  170. flock($f,LOCK_SH);
  171. while ($line = fgets($f,1024)) {
  172. $tmpLine = explode("\x0E",$line);
  173. if ($tmpLine[$column] != $query_string) {
  174. $newfile .= $line;
  175. }
  176. }
  177. fclose($f);
  178. $f = fopen($this->file,"w");
  179. flock($f,LOCK_EX);
  180. fputs($f,$newfile);
  181. fclose($f);
  182. }
  183. //Get The maximum value of a field in a file
  184. function get_max_value($column) {
  185. $tlines = file($this->file);
  186. for ($i=0;$i<=count($tlines);$ i++) {
  187. $line=explode("\x0E",$tlines[$i]);
  188. $get_value[]=$line[$column];
  189. }
  190. $get_max_value = max($get_value);
  191. return $ get_max_value;
  192. }
  193. // Query based on whether a field in the data file contains $query_string, and return all qualified data in a two-dimensional array
  194. function select($column, $query_string) {
  195. $tline = $this-> ;openfile();
  196. $lines = array();
  197. foreach ($tline as $line) {
  198. if ($line[$column] == $query_string) {
  199. array_push($lines, $line);
  200. }
  201. }
  202. return $lines;
  203. }
  204. // The function is the same as function select(), the speed may be slightly improved
  205. function select2($column, $query_string) {
  206. if (file_exists($this->file)) {
  207. $tline = $this-> read_file();
  208. foreach ($tline as $tmpLine) {
  209. $line = $this->make_array($tmpLine);
  210. if ($line[$column] == $query_string) {
  211. $lines[]= $tmpLine;
  212. }
  213. }
  214. }
  215. return $lines;
  216. }
  217. // Query based on whether a field in the data file contains $query_string, and return the first qualified data in a one-dimensional array
  218. function select_line($ column, $query_string) {
  219. $tline = $this->read_file();
  220. foreach ($tline as $tmpLine) {
  221. $line = $this->make_array($tmpLine);
  222. if ($line[ $column] == $query_string) {
  223. return $line;
  224. break;
  225. }
  226. }
  227. }
  228. // select next/prev line(next_prev ==> 1/next, 2/prev) by cx
  229. function select_next_prev_line ($column, $query_string, $next_prev) {
  230. $tline = $this->read_file();
  231. $line_key_end = count($tline) - 1;
  232. $line_key = -1;
  233. foreach ($tline as $ tmpLine) {
  234. $line_key++;
  235. $line = $this->make_array($tmpLine);
  236. if ($next_prev == 1) {
  237. // next?
  238. if ($line[$column] == $query_string ) {
  239. if ($line_key == 0) {
  240. return 0;
  241. } else {
  242. $line_key_up = $line_key - 1;
  243. return $up_line;
  244. }
  245. } else {
  246. $up_line = $line;
  247. }
  248. } elseif ($next_prev == 2) {
  249. // prev?
  250. if ($line[$column] == $query_string) {
  251. if ($line_key == $line_key_end) {
  252. return 0;
  253. } else {
  254. $line_key_down = $line_key + 1;
  255. break;
  256. }
  257. }
  258. } else {
  259. return 0;
  260. }
  261. }
  262. $down_line = $this->make_array($tline[$line_key_down]);
  263. return $down_line ;
  264. }
  265. ?>
Copy code

Text file, php


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template