search
HomeBackend DevelopmentPHP TutorialPHP classes for text file operations

  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;$iindex;$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;$iindex;$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 $line=explode("\x0E",$tlines[$i]);
  187. $get_value[]=$line[$column];
  188. }
  189. $get_max_value = max($get_value);
  190. return $ get_max_value;
  191. }
  192. // Query based on whether a field in the data file contains $query_string, and return all qualified data in a two-dimensional array
  193. function select($column, $query_string) {
  194. $tline = $this-> ;openfile();
  195. $lines = array();
  196. foreach ($tline as $line) {
  197. if ($line[$column] == $query_string) {
  198. array_push($lines, $line);
  199. }
  200. }
  201. return $lines;
  202. }
  203. // The function is the same as function select(), the speed may be slightly improved
  204. function select2($column, $query_string) {
  205. if (file_exists($this->file)) {
  206. $tline = $this-> read_file();
  207. foreach ($tline as $tmpLine) {
  208. $line = $this->make_array($tmpLine);
  209. if ($line[$column] == $query_string) {
  210. $lines[]= $tmpLine;
  211. }
  212. }
  213. }
  214. return $lines;
  215. }
  216. // Query based on whether a field in the data file contains $query_string, and return the first qualified data in a one-dimensional array
  217. function select_line($ column, $query_string) {
  218. $tline = $this->read_file();
  219. foreach ($tline as $tmpLine) {
  220. $line = $this->make_array($tmpLine);
  221. if ($line[ $column] == $query_string) {
  222. return $line;
  223. break;
  224. }
  225. }
  226. }
  227. // select next/prev line(next_prev ==> 1/next, 2/prev) by cx
  228. function select_next_prev_line ($column, $query_string, $next_prev) {
  229. $tline = $this->read_file();
  230. $line_key_end = count($tline) - 1;
  231. $line_key = -1;
  232. foreach ($tline as $ tmpLine) {
  233. $line_key++;
  234. $line = $this->make_array($tmpLine);
  235. if ($next_prev == 1) {
  236. // next?
  237. if ($line[$column] == $query_string ) {
  238. if ($line_key == 0) {
  239. return 0;
  240. } else {
  241. $line_key_up = $line_key - 1;
  242. return $up_line;
  243. }
  244. } else {
  245. $up_line = $line;
  246. }
  247. } elseif ($next_prev == 2) {
  248. // prev?
  249. if ($line[$column] == $query_string) {
  250. if ($line_key == $line_key_end) {
  251. return 0;
  252. } else {
  253. $line_key_down = $line_key + 1;
  254. break;
  255. }
  256. }
  257. } else {
  258. return 0;
  259. }
  260. }
  261. $down_line = $this->make_array($tline[$line_key_down]);
  262. return $down_line ;
  263. }
  264. ?>
Copy code

Text file, php


Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use