Home  >  Article  >  Backend Development  >  问个简单的文本处理有关问题

问个简单的文本处理有关问题

WBOY
WBOYOriginal
2016-06-13 09:58:52795browse

问个简单的文本处理问题
我想用PHP,检测 aa.txt 是否超过 50 行,如超过,就将50行以下的内容全部删除。

请问如何实现,谢谢!

------解决方案--------------------
$f_name='aa.txt';
if (($bufs = file($f_name)) !== FALSE) {
if (count($bufs > 50)) {
$s50 = '';
for ($i = 0; $i $s50 .= $bufs[$i];
}
file_put_contents($f_name, $s50);//PHP 5+
}
}
------解决方案--------------------

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('写入错误');      else echo '写入成功';      fclose($handle);   }}<div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn