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 );}}