PHP class for text file operations

Source: Internet
Author: User
Tags chop flock
  1. var $file;
  2. var $index;
  3. Create a file and write the 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 record to end of 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 to a one-dimensional array
  18. function Make_array ($line) {
  19. $array = Explode ("\\x0E", $line);
  20. return $array;
  21. }
  22. Converts a row of data for a one-dimensional array
  23. function Join_array ($line) {
  24. $array = Join ("\\x0E", $line); return $array;
  25. }
  26. Returns the total number of data file rows
  27. function Getlines () {
  28. $f =file ($this->file);
  29. return count ($f);
  30. }
  31. Returns the data record for the next row (alternate)
  32. function Next_line () {
  33. $this->index= $this->index++;
  34. return $this->get ();
  35. }
  36. Returns the data record for the previous row (alternate)
  37. function Prev_line () {
  38. $this->index= $this->index--;
  39. return $this->get ();
  40. }
  41. Returns the data record data for the current row 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. Returns the data record data for the current row is large
  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 file contents in 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 Data File---Return file contents in 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 into one row of data, 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 records to the end of a 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 eligible data records for large byte-per-row data
  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
  122. $list = Explode ("\\x0E", $FC [$i]);
  123. if ($list [$column]! = $query _string) {
  124. $newfile = $newfile. Chop ($FC [$i]). " \\n ";
  125. } else {
  126. $newfile = $newfile. $update _string;
  127. }
  128. }
  129. Fclose ($f);
  130. $f =fopen ($this->file, "w");
  131. Flock ($f, LOCK_EX);
  132. Fputs ($f, $newfile);
  133. Fclose ($f);
  134. }
  135. Update all eligible data records for smaller byte data per row
  136. function Update2 ($column, $query _string, $update _array) {
  137. $newline = Implode ("\\x0E", $update _array);
  138. $newfile = "";
  139. $f = fopen ($this->file, "R");
  140. Flock ($f, lock_sh);
  141. while ($line = fgets ($f, 1024)) {
  142. $tmpLine = Explode ("\\x0E", $line);
  143. if ($tmpLine [$column] = = $query _string) {
  144. $newfile. = $newline;
  145. } else {
  146. $newfile. = $line;
  147. }
  148. }
  149. Fclose ($f);
  150. $f = fopen ($this->file, "w");
  151. Flock ($f, LOCK_EX);
  152. Fputs ($f, $newfile);
  153. Fclose ($f);
  154. }
  155. Delete all eligible data records for large byte-per-row data
  156. function Delete ($column, $query _string) {
  157. $newfile = "";
  158. $FC =file ($this->file);
  159. $f =fopen ($this->file, "R");
  160. Flock ($f, lock_sh);
  161. for ($i =0; $i
  162. $list = Explode ("\\x0E", $FC [$i]);
  163. if ($list [$column]! = $query _string) {
  164. $newfile = $newfile. Chop ($FC [$i]). " \\n ";
  165. }
  166. }
  167. Fclose ($f);
  168. $f =fopen ($this->file, "w");
  169. Flock ($f, LOCK_EX);
  170. Fputs ($f, $newfile);
  171. Fclose ($f);
  172. }
  173. Delete all eligible data records for smaller byte data per row
  174. function Delete2 ($column, $query _string) {
  175. $newfile = "";
  176. $f = fopen ($this->file, "R");
  177. Flock ($f, lock_sh);
  178. while ($line = fgets ($f, 1024)) {
  179. $tmpLine = Explode ("\\x0E", $line);
  180. if ($tmpLine [$column]! = $query _string) {
  181. $newfile. = $line;
  182. }
  183. }
  184. Fclose ($f);
  185. $f = fopen ($this->file, "w");
  186. Flock ($f, LOCK_EX);
  187. Fputs ($f, $newfile);
  188. Fclose ($f);
  189. }
  190. Get the maximum value of a field in a file
  191. function Get_max_value ($column) {
  192. $tlines = File ($this->file);
  193. for ($i =0; $i <=count ($tlines); $i + +) {
  194. $line =explode ("\\x0E", $tlines [$i]);
  195. $get _value[]= $line [$column];
  196. }
  197. $get _max_value = max ($get _value);
  198. return $get _max_value;
  199. }
  200. Returns all eligible data in a two-dimensional array, based on whether a field in the data file contains $query_string
  201. function Select ($column, $query _string) {
  202. $tline = $this->openfile ();
  203. $lines = Array ();
  204. foreach ($tline as $line) {
  205. if ($line [$column] = = $query _string) {
  206. Array_push ($lines, $line);
  207. }
  208. }
  209. return $lines;
  210. }
  211. Functions like function Select (), speed may be slightly improved
  212. function Select2 ($column, $query _string) {
  213. if (file_exists ($this->file)) {
  214. $tline = $this->read_file ();
  215. foreach ($tline as $tmpLine) {
  216. $line = $this->make_array ($tmpLine);
  217. if ($line [$column] = = $query _string) {
  218. $lines []= $tmpLine;
  219. }
  220. }
  221. }
  222. return $lines;
  223. }
  224. Returns the first qualifying data in a one-dimensional array, based on whether a field in the data file contains $query_string
  225. function Select_line ($column, $query _string) {
  226. $tline = $this->read_file ();
  227. foreach ($tline as $tmpLine) {
  228. $line = $this->make_array ($tmpLine);
  229. if ($line [$column] = = $query _string) {
  230. return $line;
  231. Break
  232. }
  233. }
  234. }
  235. Select Next/prev Line (Next_prev ==> 1/next, 2/prev) by CX
  236. function Select_next_prev_line ($column, $query _string, $next _prev) {
  237. $tline = $this->read_file ();
  238. $line _key_end = count ($tline)-1;
  239. $line _key =-1;
  240. foreach ($tline as $tmpLine) {
  241. $line _key++;
  242. $line = $this->make_array ($tmpLine);
  243. if ($next _prev = = 1) {
  244. Next?
  245. if ($line [$column] = = $query _string) {
  246. if ($line _key = = 0) {
  247. return 0;
  248. } else {
  249. $line _key_up = $line _key-1;
  250. return $up _line;
  251. }
  252. } else {
  253. $up _line = $line;
  254. }
  255. } elseif ($next _prev = = 2) {
  256. Prev?
  257. if ($line [$column] = = $query _string) {
  258. if ($line _key = = $line _key_end) {
  259. return 0;
  260. } else {
  261. $line _key_down = $line _key + 1;
  262. Break
  263. }
  264. }
  265. } else {
  266. return 0;
  267. }
  268. }
  269. $down _line = $this->make_array ($tline [$line _key_down]);
  270. return $down _line;
  271. }
  272. ?>
Copy Code
Text files, PHP
  • Related Article

    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.