Recommended efficient methods for PHP to read N rows at the end of a large file. _ PHP Tutorial

Source: Internet
Author: User
It is recommended to use PHP to read N rows at the end of a large file ,. It is recommended to use PHP to read N rows at the end of a large file. you can use the file () function to read files into arrays by row, when array_pop is used to obtain the last row, it is recommended to use PHP to read N rows at the end of a large file,

You can use the file () function to read files within a few megabytes into an array by row. you can use array_pop to get the last row.

However, for large text files, the machine memory is not large enough, or php itself has a memory_limit limit, this method is not applicable, even if the force is not limited, the efficiency is very low.

No way? Of course there are, but there are no ready-made functions, you need to do it yourself.

Here we need to use the file pointer. if I have learned C, I should know the pointer. In other words, I open a file through fopen in PHP. at this time, the file has not been read, at this time, it points to the beginning of the file, and the pointer position is 0. when you read the content from the file through fgets or fgetc, The Pointer also advances accordingly.

while(!feof($fp)){$data.=fgets($fp,4096);}

The implementation principle is that fgets reads the specified length string from the position of the current pointer until a line break is encountered.

Can I control the pointer position to the nth row at the bottom? Unfortunately, no, but the pointer can be directly moved to the end, and N positions can be regressed through the fseek () function.

First, we move the pointer to the end, and move backward to two positions. we read a character through fgetc and determine whether the character is "\ n" or a line break. if it is not a line break, then, continue to return to a position and judge again until we go back to the end of the last line, and use fgets to retrieve all rows. Two while loops are used here. the outer loop controls the number of rows to be obtained, and the inner loop controls the fseek action.

The function is as follows:

/*** Get the last $ n line of the file * @ param string $ filename File path * @ param int $ n the last few lines * @ return mixed false indicates an error occurred, returns the string */function FileLastLines ($ filename, $ n) {if (! $ Fp = fopen ($ filename, 'r') {echo "failed to open the file. Please check whether the file path is correct and the path and file name do not contain Chinese characters"; return false ;} $ pos =-2; $ eof = ""; $ str = ""; while ($ n> 0) {while ($ eof! = "\ N") {if (! Fseek ($ fp, $ pos, SEEK_END) {$ eof = fgetc ($ fp); $ pos --;} else {break;} $ str. = fgets ($ fp); $ eof = ""; $ n --;} return $ str;} echo nl2br(FileLastLines('sss.txt ', 4 ));

The efficiency recommendation for reading N rows at the end of a large file in PHP is all the content shared by the editor. I hope you can give us a reference and support for the help house.

Bytes, the size of a small file within a few megabytes, you can use the file () function to read the file into the array by row, get the last row with array_pop, just...

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.