Php class for text file operations

Source: Internet
Author: User
Tags chop flock
Php class for text file operations

  1. Var $ file;
  2. Var $ index;
  3. // Create a file and write it into 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 records to the end of the file
  11. Function add_write ($ new ){
  12. $ F = fopen ($ this-> file, "");
  13. Flock ($ f, LOCK_EX );
  14. Fputs ($ f, $ new );
  15. Fclose ($ f );
  16. }
  17. // Used together with the returned results 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 a row of data into a one-dimensional array
  23. Function join_array ($ line ){
  24. $ Array = join ("\ x0E", $ line); return $ array;
  25. }
  26. // Total number of returned data files
  27. Function getlines (){
  28. $ F = file ($ this-> file );
  29. Return count ($ f );
  30. }
  31. // Return the data record of the next row (standby)
  32. Function next_line (){
  33. $ This-> index = $ this-> index ++;
  34. Return $ this-> get ();
  35. }
  36. // Return the data record of the previous row (standby)
  37. Function prev_line (){
  38. $ This-> index = $ this-> index --;
  39. Return $ this-> get ();
  40. }
  41. // The returned data record 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. // The returned data of 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 the data file --- return the file content in 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 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. // Input an array, merge it into a row of data, and overwrite 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 the file
  91. Function add_line ($ array, $ check_n = 1 ){
  92. $ S = implode ("\ x0E", $ array );
  93. $ F = fopen ($ this-> file, "");
  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 data record to the beginning 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, applicable to the large data size in each row
  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 data records that meet the conditions, applicable to the case where each row has small bytes of data
  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 );
  153. }
  154. // Delete all data records that meet the conditions, which is suitable for large bytes of data in each row.
  155. Function delete ($ column, $ query_string ){
  156. $ Newfile = "";
  157. $ Fc = file ($ this-> file );
  158. $ F = fopen ($ this-> file, "r ");
  159. Flock ($ f, LOCK_SH );
  160. For ($ I = 0; $ I $ List = explode ("\ x0E", $ fc [$ I]);
  161. If ($ list [$ column]! = $ Query_string ){
  162. $ Newfile = $ newfile. chop ($ fc [$ I]). "\ n ";
  163. }
  164. }
  165. Fclose ($ f );
  166. $ F = fopen ($ this-> file, "w ");
  167. Flock ($ f, LOCK_EX );
  168. Fputs ($ f, $ newfile );
  169. Fclose ($ f );
  170. }
  171. // Delete all data records that meet the conditions, applicable to the case where the data in each row is small
  172. Function delete2 ($ column, $ query_string ){
  173. $ Newfile = "";
  174. $ F = fopen ($ this-> file, "r ");
  175. Flock ($ f, LOCK_SH );
  176. While ($ line = fgets ($ f, 1024 )){
  177. $ TmpLine = explode ("\ x0E", $ line );
  178. If ($ tmpLine [$ column]! = $ Query_string ){
  179. $ Newfile. = $ line;
  180. }
  181. }
  182. Fclose ($ f );
  183. $ F = fopen ($ this-> file, "w ");
  184. Flock ($ f, LOCK_EX );
  185. Fputs ($ f, $ newfile );
  186. Fclose ($ f );
  187. }
  188. // Obtain the maximum value of a field in a file
  189. Function get_max_value ($ column ){
  190. $ Tlines = file ($ this-> file );
  191. For ($ I = 0; $ I <= count ($ tlines); $ I ++ ){
  192. $ Line = explode ("\ x0E", $ tlines [$ I]);
  193. $ Get_value [] = $ line [$ column];
  194. }
  195. $ Get_max_value = max ($ get_value );
  196. Return $ get_max_value;
  197. }
  198. // Query based on whether a field in the data file contains $ query_string. all qualified data is returned using a two-dimensional array.
  199. Function select ($ column, $ query_string ){
  200. $ Tline = $ this-> openfile ();
  201. $ Lines = array ();
  202. Foreach ($ tline as $ line ){
  203. If ($ line [$ column] ==$ query_string ){
  204. Array_push ($ lines, $ line );
  205. }
  206. }
  207. Return $ lines;
  208. }
  209. // Functions are the same as function select (), and the speed may be slightly improved
  210. Function select2 ($ column, $ query_string ){
  211. If (file_exists ($ this-> file )){
  212. $ Tline = $ this-> read_file ();
  213. Foreach ($ tline as $ tmpLine ){
  214. $ Line = $ this-> make_array ($ tmpLine );
  215. If ($ line [$ column] ==$ query_string ){
  216. $ Lines [] = $ tmpLine;
  217. }
  218. }
  219. }
  220. Return $ lines;
  221. }
  222. // Query whether a field in the data file contains $ query_string. the first qualified data is returned in a one-dimensional array.
  223. Function select_line ($ column, $ query_string ){
  224. $ Tline = $ this-> read_file ();
  225. Foreach ($ tline as $ tmpLine ){
  226. $ Line = $ this-> make_array ($ tmpLine );
  227. If ($ line [$ column] ==$ query_string ){
  228. Return $ line;
  229. Break;
  230. }
  231. }
  232. }
  233. // Select next/prev line (next_prev => 1/next, 2/prev) by cx
  234. Function select_next_prev_line ($ column, $ query_string, $ next_prev ){
  235. $ Tline = $ this-> read_file ();
  236. $ Line_key_end = count ($ tline)-1;
  237. $ Line_key =-1;
  238. Foreach ($ tline as $ tmpLine ){
  239. $ Line_key ++;
  240. $ Line = $ this-> make_array ($ tmpLine );
  241. If ($ next_prev = 1 ){
  242. // Next?
  243. If ($ line [$ column] ==$ query_string ){
  244. If ($ line_key = 0 ){
  245. Return 0;
  246. } Else {
  247. $ Line_key_up = $ line_key-1;
  248. Return $ up_line;
  249. }
  250. } Else {
  251. $ Up_line = $ line;
  252. }
  253. } Elseif ($ next_prev = 2 ){
  254. // Prev?
  255. If ($ line [$ column] ==$ query_string ){
  256. If ($ line_key = $ line_key_end ){
  257. Return 0;
  258. } Else {
  259. $ Line_key_down = $ line_key + 1;
  260. Break;
  261. }
  262. }
  263. } Else {
  264. Return 0;
  265. }
  266. }
  267. $ Down_line = $ this-> make_array ($ tline [$ line_key_down]);
  268. Return $ down_line;
  269. }
  270. ?>


Text file, php

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.