How does PHP obtain the number of file lines?

Source: Internet
Author: User
It seems that no function is found to directly obtain the number of objects. Can I only count rows by row? If the system (& quot; wc-l file name & quot;) for large files is efficient, it seems that no function is found to directly obtain the number of file lines.

Can I only count rows by row?

Is the direct sys ("wc-l file name") of large files more efficient?

Reply content:

It seems that no function is found to directly obtain the number of objects.

Can I only count rows by row?

Is the direct sys ("wc-l file name") of large files more efficient?

Small files directly$ Line = count (file ('filename '));
Large files can be usedSplFileObjectProcessing.

Row-by-row counting slows down when the number of rows is large. For example, if you use fgets to read a row by default, each row generates function call overhead.

You can use substr_count ($ str, "\ n") to count the number of carriage returns.

For large files, you can read 1 MB segments each time, then use substr_count to count the number of trips, and then accumulate the results.

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.