PHP reads the content of the txt file and assigns it to the array code

Source: Internet
Author: User
Use file_get_contents () to obtain the content of the txt file, and then convert the obtained string to an array through explode. You can use the count () function to obtain the array length.

Use file_get_contents () to obtain the content of the txt file, and then convert the obtained string to an array through explode. You can use the count () function to obtain the array length.

The content of the file 2010-12-15.txt is as follows:
The Code is as follows:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
T01
T02
T03
T04
T05
T06
T07
T08
T09
T10
T11
T12
T13
T14
T15
T16

The idea is as follows: Use file_get_contents () to get the content of the txt file, and then convert the obtained string to an array through explode. You can use the count () function to obtain the array length.

PHP Code
The Code is as follows:
$ File = '2010-12-15.txt ';
$ Content = file_get_contents ($ file );
// Echo $ content;

$ Array = explode ("\ r \ n", $ content );
// Print_r ($ array );

For ($ I = 0; $ I <count ($ array); $ I ++)
{
Echo $ array [$ I]. '<br/> /〉';
}

Line breaks in txt documents
In a regular expression, \ n indicates newline, and \ r indicates carriage return (that is, this leads to an idiot Chinese translation "Press ENTER. When processing String or console output, line breaks can be applied to any String or console output.

However, in txt, none of them are the standard line breaks. Only the combined \ r \ n is the line feed (this is the case for the default line breaks of windows ).

It means \ r knocks on the carriage return, indicating that this line is over, the cursor goes back to the header, and then \ n moves down a new line.

Because of this, when I used lift () to search for a txt document with a regular expression, I couldn't find the problem at all.

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 file content into a string. If supported by the operating system, the memory ing technology will be used to enhance the performance.
Related Article

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.