php实现TXT小说章节解析、小说章节在线阅读

ε祈祈猫儿з 提交于 2020-12-08 08:31:26

每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

 

要实现TXT文本章节的解析,大概思路是在每个章节加入了特定的字符,然后根据字符的起始位置读取章节。这里我写了一个小说阅读项目,也是根据这个思路进行。

实现步骤:

    1. 解析TXT文件,生成章节内容

                   1. 编辑TXT文件,在每个章节名称加入我自己定义的一个字符串,用以识别。

                   2.识别章节,获取到章节列表

                   3.根据自定义的字符串,循环读取每个章节的内容,并生成每个章节的TXT文件

            2.获取章节列表,实现获取章节内容接口

                   1. 章节列表接口

                   2. 章节内容接口

 

源码:

1.解析TXT文件,生成章节内容

 1 <?php
 2 header("content-type:text/html;charset=utf-8");
 3 
 4 $artName = $_GET["artname"];
 5 
 6 if(!$artName){
 7     echo "文件名没有哦";exit();
 8 }
 9 
10 $fromFileName = "../".$artName.".txt";
11 if(!file_exists($fromFileName)){
12     echo "源文件不存在啊";exit();
13 }
14 
15 $distDirName = $artName;
16 
17 $myfile = fopen($fromFileName, "r") or die("Unable to open file!");
18 $content = fread($myfile,filesize($fromFileName));
19 fclose($myfile);
20 echo "<br>读取原始文件成功..............";
21 
22 $pattern='/#titlestart#(.*?)#titleend#/is';
23 preg_match_all ($pattern, $content, $result, PREG_PATTERN_ORDER);
24 
25 echo "<br><br>文章目录识别成功..............<br><br>";
26 
27 // 获取到目录
28 // 目录数组 $result[1]
29 $catalogStr = "";
30 $catalogArr = array();
31 foreach($result[1] as $v){
32     array_push($catalogArr,$v);
33     $catalogStr .= $v."#catalog#";
34 }
35 
36 // 创建书本目录
37 $dir = iconv("UTF-8", "GBK", "./books/".$distDirName);
38 if (!file_exists($dir)){
39     mkdir ($dir,0777,true);
40     echo '<br>创建文件夹bookdir成功<br><br>';
41 } else {
42     echo '<br>需创建的文件夹bookdir已经存在<br><br>';
43 }
44 
45 // 生成目录文件
46 $myfile = fopen($dir."/catalog.txt", "w") or die("Unable to open file!");
47 fwrite($myfile, $catalogStr);
48 fclose($myfile);
49 echo "<br>==============================目录文件生成成功..............<br>";
50 
51 // 获取到内容,写入文件
52 foreach($catalogArr as $k=>$v){
53     $pattern='/#titlestart#'.$v.'#titleend#(.*?)#titlestart/is';
54     preg_match ($pattern, $content, $result);
55     
56     $myfile = fopen($dir."/".($k+1).".txt", "w") or die("Unable to open file!");
57     fwrite($myfile, $result[1]);
58     fclose($myfile);
59     echo "<br>===================文章第".($k+1)."章节写入成功..............";
60 }
61 
62 
63 echo "<br><br><br><br><br><br><br>====================书本识别成功...........................";
64 
65 
66 
67 ?>

 

      2.获取章节列表,实现获取章节内容接口

 1 <?php
 2 header('Content-Type:application/json');
 3 
 4 $action = $_GET["act"];
 5 $fileName = $_GET["artname"];
 6 
 7 if(!$action){
 8     $rsp = array("code" => 500, "msg" => "请求不存在");
 9     $rsp = json_encode($rsp);
10     echo $rsp;
11     exit();
12 }else if(!$fileName){
13     $rsp = array("code" => 500, "msg" => "书本不存在");
14     $rsp = json_encode($rsp);
15     echo $rsp;
16     exit();
17 }
18 
19 if($action == "getcat"){
20     // 获取目录
21     $fileName = "../books/".$fileName."/catalog.txt";
22 
23     // 读取文件
24     if(file_exists($fileName)){
25         $myfile = fopen($fileName, "r") or die("Unable to open file!");
26         $content = fread($myfile,filesize($fileName));
27         fclose($myfile);
28 
29         $code = 200;
30         $msg = "读取目录成功";
31         $catalogArray = array();
32         if($content){
33             $carray = explode('#catalog#',$content); 
34             
35             foreach($carray as $k=>$v){
36                 if($v){
37                     $item = array("index"=>($k+1), "cataTitle"=>$v);
38                     array_push($catalogArray, $item);
39                 }
40             }
41         }
42         
43         if(count($catalogArray)<=0){
44             $code = 500;
45             $msg = "书本不存在目录";
46         }
47         
48         $rsp = array("code" => $code, "msg" => $msg, "catalogList" => $catalogArray);
49         $rsp = json_encode($rsp);
50         
51         echo $rsp;
52         exit();
53     }else{
54         // 文件不存在
55         $rsp = array("code" => 404, "msg" => "文件不存在");
56         $rsp = json_encode($rsp);
57         echo $rsp;
58         exit();
59     }
60 }else if($action=="getcon"){
61     // 获取文章内容
62     $index = (int)$_GET["index"];
63     
64     $fileName = "../books/".$fileName."/".$index.".txt";
65     
66     if(file_exists($fileName)){
67         $myfile = fopen($fileName, "r") or die("Unable to open file!");
68         $content = fread($myfile,filesize($fileName));
69         fclose($myfile);
70         
71         $content = str_replace("\r\n","<br>", $content);
72         $rsp = array("code" => 200, "msg" => "读取内容成功", "con" => $content);
73         $rsp = json_encode($rsp);
74         
75         echo $rsp;
76         exit();
77     }else{
78         // 文件不存在
79         $rsp = array("code" => 404, "msg" => "文件不存在");
80         $rsp = json_encode($rsp);
81         echo $rsp;
82         exit();
83     }
84 }else{
85     echo "error request, please check your request content";
86     exit();
87 }
88 
89 
90 ?>

 

 

 

每天学习一点点 编程PDF电子书、视频教程免费下载:
http://www.shitanlife.com/code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!