Home Backend Development PHP Tutorial PHP classes for text file operations

PHP classes for text file operations

Jul 25, 2016 am 08:43 AM

  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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles