PHP reads the contents of the TXT file and assigns the code to the array _php tutorial

Source: Internet
Author: User
Tags explode
The contents of the 2010-12-15.txt file are as follows:
Copy CodeThe code is as follows:
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
T01
T02
T03
T04
T05
T06
T07
T08
T09
T10
T11
T12
T13
T14
T15
T16

Here's the idea: Use File_get_contents () to get the contents of the TXT file, and then convert the obtained string to an array by explode (). You can use the count () function to get the array length.

PHP Code
Copy CodeThe 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/〉 ';
}

About line breaks in TXT documents
In the regular expression, there is a \ n is the meaning of newline, and a \ r is carriage return (that is, this led to the idiot Chinese translation "return") meaning. When working with a string or console output, it can be wrapped in any line.

But in TXT, which is not the standard line, only the combination of \ r \ n is a newline (for the entire Windows default line-wrapping, this is the case).

It means that the line is over, the cursor goes back to the head, and then it moves down a line to a new line.

Because of this, I used scanner () with regular expressions to search for TXT documents, once helpless, there is no 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 contents of a file into a string. If supported by the operating system, memory-mapping techniques are also used to enhance performance.

http://www.bkjia.com/PHPjc/324445.html www.bkjia.com true http://www.bkjia.com/PHPjc/324445.html techarticle the file contents of 2010-12-15.txt are as follows: Copy the code code as follows: in Geneva, the following is a one-to-one-ten-a-T01 T02 T03 T04 T05 T06 T07 T08 T09 T10 T11 ...

  • 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.