How to Use php to read n rows at the end of a file

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

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

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.

/*** 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 ));

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.