This article mainly introduces the PHP read TXT file content conversion array and the number of rows to obtain the specified data, can be used to implement the large data TXT file paging display save, involving PHP file_get_contents, A simple way to manipulate the explode functions and to convert the TXT content to an array. You need a friend to refer to.
I. Introduction to PHP operation TXT file related functions
① about file_get_contents ()
The file_get_contents () function reads the entire file into a string.
The file_get_contents () function is the preferred method for reading the contents of a file into a string. If supported by the operating system, memory-mapping techniques are also used to enhance performance.
② about Explode ()
explode-using one string to split another string
The explode () function splits the string into arrays with the specified delimiter.
③ about PHP for loop statements
The For loop is one of the most commonly used looping statements in our work, in any language, with this looping statement.
Syntax rules:
for (EX1; ex2; ex3) {
The code to execute
}
Ex1: Indicates where the loop begins
EX2: The condition of the loop, if the value is TRUE, the loop continues, and the nested loop statement is executed. If the value is FALSE, the loop is terminated.
EX3: evaluated (and executed) after each loop.
Second: PHP read TXT file content conversion array and the number of rows to get the specified data specific method
The sample code is as follows:
<?php$str = file_get_contents (' url.txt '); Reads the contents of the Url.txt file into a string $array = Explode ("\ r \ n", $str); Divide the string into arrays with a newline delimiter (\ r \ n), dividing each row into a value for the array ($i =0; $i <200; $i + +) { echo $array [$i]; echo "<br>";} Output the first 200 lines of the Url.txt text with a For loop statement, and use the BR tag to wrap the display or for ($i =0; $i <400; $i + +) { echo $array [$i +200]; echo "<br>";} Output the contents of line No. 200 to No. 400 of the url.txt text or echo $array [3]; Outputs the 4th line of the Url.txt text (the array's key values are starting from 0)?>
It is hoped that this article will be helpful for you to use simple PHP method to get text to specify content and realize txt text paging display preservation.