How to convert TXT file content to an array and get the specified content by number of lines in PHP (example)

Source: Internet
Author: User
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.

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.