php指针处理小说章节

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容