Ask a simple text processing question

Source: Internet
Author: User
I want to use PHP to check whether there are more than 50 lines of aa.txt. if there are more than 50 lines of aa.txt, delete all the content below 50 lines. How can this problem be achieved? thank you !, When f_name='aa.txt '; if ($ bufs = file ($ f_name ))! = FALSE) {if (count ($ bufs & gt; 50) ask a simple text processing question
I want to use PHP to check if there are more than 50 lines of aa.txt. if there are more than 50 lines, delete all the content below 50 lines.

How can this problem be achieved? thank you!

------ Solution --------------------
Using f_name='aa.txt ';
If ($ bufs = file ($ f_name ))! = FALSE ){
If (count ($ bufs> 50 )){
$ S50 = '';
For ($ I = 0; $ I <50; $ I ++ ){
$ S50. = $ bufs [$ I];
}
File_put_contents ($ f_name, $ s50); // PHP 5 +
}
}
------ Solution --------------------
PHP code
$ F = file('aa.txt '); if (count ($ f)> 50) {$ f = array_slice ($ f, 0, 49); if (is_writeable('aa.txt ')) {$ handle = fopen('aa.txt ', 'wb'); if (fwrite ($ handle, implode ('', $ f) === false) exit ('write error '); else echo 'write succeeded '; fclose ($ handle );}}

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.