PHP reads the contents of the TXT file and assigns a value to the array's code

Source: Internet
Author: User

Use File_get_contents () to get the contents of the TXT file, and then convert the obtained string to an array by explode ().

Get array length You can use the count () function

<?PHP$file= ' Keywords.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 the meaning of carriage return.

When working with a string or console output, it can be wrapped in any line.

But in TXT, which is not the standard line-break, 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.

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.

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.