phpʵÏÖ¶ÁÈ¡³¬´óÎļþµÄ·½·¨

5年以前  |  阅读数:242 次  |  编程语言:PHP 

ͨ³£À´ËµÔÚphp¶ÁÈ¡´oÎļþµÄʱºo£¬ÎÒÃDzÉÓõĽ¨Ò»°aÊÇÒ»ÐÐÐÐÀ´½²È¡£¬¶ø²»ÊÇÒ»´ÎÐÔ°ÑÎļþÈ«²¿Ð´ÈeÄÚ´aeÖУ¬ÕaÑu»aµ¼ÖÂphp³ÌÐo¿¨ËÀ£¬ÏÂÃae¾Í¸ø´o¼Ò½eÉÜÕaÑuÒ»¸oÀý×Ó¡£

¶ÁÈ¡´oÎļþ×iºo¼¸ÐÐÊý¾Ý£º


    <?php
    /**
     * È¡Îļþ×iºo$nÐÐ
     * 
     * @param string $filename ÎļþÂ*¾¶
     * @param int $n ×iºo¼¸ÐÐ
     * @return mixed false±iʾÓдiÎo£¬³É¹¦Ôo*µ»Ø×Ö*u´®
     */
    function FileLastLines($filename, $n){
       if(!$fp = fopen($filename, 'r')){
        echo "´o¿ªÎļþʧ°Ü£¬Çe¼i²eÎļþÂ*¾¶ÊÇ*ñÕýÈ*£¬Â*¾¶ºÍÎļþÃu²»Òª°uº¬ÖÐÎÄ";
        return false;
        }
      $pos = -2;
      $eof = "";
      $str = "";
      while($n > 0){
        while($eof != "n"){
          if(!fseek($fp, $pos, SEEK_END)){
            $eof = fgetc($fp);
            $pos¨C;
            }else{
            break;
            }
          }
        $str .= fgets($fp);
        $eof = "";
        $n¨C;
        }
      return $str;
      }

    echo nl2br(FileLastLines('sss.txt', 4));
    /**
     * * * È¡Îļþ×iºo$nÐÐ *
     * 
     * @param string $filename ÎļþÂ*¾¶ *
     * @param int $n ×iºo¼¸ÐÐ *
     * @return mixed false±iʾÓдiÎo£¬³É¹¦Ôo*µ»Ø×Ö*u´®
     */
    function FileLastLines($filename, $n){
      if(!$fp = fopen($filename, 'r')){
        echo "´o¿ªÎļþʧ°Ü£¬Çe¼i²eÎļþÂ*¾¶ÊÇ*ñÕýÈ*£¬Â*¾¶ºÍÎļþÃu²»Òª°uº¬ÖÐÎÄ";
        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));

    function tail($fp, $n, $base = 5)
    {
      assert($n > 0);
      $pos = $n + 1;
      $lines = array();
      while(count($lines) < = $n){
        try{
          fseek($fp, - $pos, SEEK_END);
          }
        catch (Exception $e){
          fseek(0);
          break;
          }
        $pos *= $base;
        while(!feof($fp)){
          array_unshift($lines, fgets($fp));
          }
        }
      return array_slice($lines, 0, $n);
      }
    var_dump(tail(fopen("access.log", "r+"), 10));
    $fp = fopen($file, "r");
    $line = 10;
    $pos = -2;
    $t = " ";
    $data = "";
    while ($line > 0){
      while ($t != "n"){
        fseek($fp, $pos, SEEK_END);
        $t = fgetc($fp);
        $pos --;
        }
      $t = " ";
      $data .= fgets($fp);
      $line --;
      }
    fclose ($fp);
    echo $data;
    ?>

¶ÁÕß¿ÉÒÔ¸u¾Ý¸ÃʵÀýÌصa¼ÓÒԸĽøºÍÍeÉÆ£¬Ê¹Ö®¸u¼Ó*uºÏ×ÔÉiÓ¦ÓÃÐeÇo¡£

 相关文章:
PHP分页显示制作详细讲解
SSH 登录失败:Host key verification failed
获取IMSI
将二进制数据转为16进制以便显示
获取IMEI
文件下载
贪吃蛇
双位运算符
PHP自定义函数获取搜索引擎来源关键字的方法
Java生成UUID
发送邮件
年的日历图
提取后缀名
在Zeus Web Server中安装PHP语言支持
让你成为最历害的git提交人
Yii2汉字转拼音类的实例代码
再谈PHP中单双引号的区别详解
指定应用ID以获取对应的应用名称
Python 2与Python 3版本和编码的对比
php封装的page分页类完整实例