php指针处理小说章节

2018-01-16  本文已影响0人  攻城大师

php通过指针来标记小说章节位置

        $fileName = "./test.txt";
        $handle = @fopen($fileName, "r");
        $arr = array();
        $temp = array();
        //临时指针
        $tempPointer = 0;
        while (!feof($handle)) {
            //获得一行,指针移位下一行
            $item = fgets($handle, 4096);
            //如果是章节,存入章节指针位置
            if(preg_match("/第[0-9一二两三四五六七八九十百千万]+章(.*)\r\n/",$item,$matches)){
                $fileChapterPointerArr[trim($item)] = $tempPointer;//指针移位,获取上一行指针
            }else{
                $tempPointer = ftell($handle);
            }
        }
        fclose($handle);
        $handle = @fopen($fileName, "r");
        foreach ($fileChapterPointerArr as $k=>$v) {
            //移动文件指针到50
            fseek($handle,$v);
            //读取50-100字节处的内容 50=100-50
            $length = next($fileChapterPointerArr)-$v>0 ? next($fileChapterPointerArr)-$v : 99999;//99999待优化
            $content = fread($handle,$length);
            echo '<hr/>';
            echo $content;
        }

上一篇下一篇

猜你喜欢

热点阅读