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

时间:2023-12-27 09:58:01

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

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

实现步骤:

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

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

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

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

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

1. 章节列表接口

2. 章节内容接口

源码:

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

 <?php
header("content-type:text/html;charset=utf-8"); $artName = $_GET["artname"]; if(!$artName){
echo "文件名没有哦";exit();
} $fromFileName = "../".$artName.".txt";
if(!file_exists($fromFileName)){
echo "源文件不存在啊";exit();
} $distDirName = $artName; $myfile = fopen($fromFileName, "r") or die("Unable to open file!");
$content = fread($myfile,filesize($fromFileName));
fclose($myfile);
echo "<br>读取原始文件成功.............."; $pattern='/#titlestart#(.*?)#titleend#/is';
preg_match_all ($pattern, $content, $result, PREG_PATTERN_ORDER); echo "<br><br>文章目录识别成功..............<br><br>"; // 获取到目录
// 目录数组 $result[1]
$catalogStr = "";
$catalogArr = array();
foreach($result[1] as $v){
array_push($catalogArr,$v);
$catalogStr .= $v."#catalog#";
} // 创建书本目录
$dir = iconv("UTF-8", "GBK", "./books/".$distDirName);
if (!file_exists($dir)){
mkdir ($dir,0777,true);
echo '<br>创建文件夹bookdir成功<br><br>';
} else {
echo '<br>需创建的文件夹bookdir已经存在<br><br>';
} // 生成目录文件
$myfile = fopen($dir."/catalog.txt", "w") or die("Unable to open file!");
fwrite($myfile, $catalogStr);
fclose($myfile);
echo "<br>==============================目录文件生成成功..............<br>"; // 获取到内容,写入文件
foreach($catalogArr as $k=>$v){
$pattern='/#titlestart#'.$v.'#titleend#(.*?)#titlestart/is';
preg_match ($pattern, $content, $result); $myfile = fopen($dir."/".($k+1).".txt", "w") or die("Unable to open file!");
fwrite($myfile, $result[1]);
fclose($myfile);
echo "<br>===================文章第".($k+1)."章节写入成功..............";
} echo "<br><br><br><br><br><br><br>====================书本识别成功..........................."; ?>

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

 <?php
header('Content-Type:application/json'); $action = $_GET["act"];
$fileName = $_GET["artname"]; if(!$action){
$rsp = array("code" => 500, "msg" => "请求不存在");
$rsp = json_encode($rsp);
echo $rsp;
exit();
}else if(!$fileName){
$rsp = array("code" => 500, "msg" => "书本不存在");
$rsp = json_encode($rsp);
echo $rsp;
exit();
} if($action == "getcat"){
// 获取目录
$fileName = "../books/".$fileName."/catalog.txt"; // 读取文件
if(file_exists($fileName)){
$myfile = fopen($fileName, "r") or die("Unable to open file!");
$content = fread($myfile,filesize($fileName));
fclose($myfile); $code = 200;
$msg = "读取目录成功";
$catalogArray = array();
if($content){
$carray = explode('#catalog#',$content); foreach($carray as $k=>$v){
if($v){
$item = array("index"=>($k+1), "cataTitle"=>$v);
array_push($catalogArray, $item);
}
}
} if(count($catalogArray)<=0){
$code = 500;
$msg = "书本不存在目录";
} $rsp = array("code" => $code, "msg" => $msg, "catalogList" => $catalogArray);
$rsp = json_encode($rsp); echo $rsp;
exit();
}else{
// 文件不存在
$rsp = array("code" => 404, "msg" => "文件不存在");
$rsp = json_encode($rsp);
echo $rsp;
exit();
}
}else if($action=="getcon"){
// 获取文章内容
$index = (int)$_GET["index"]; $fileName = "../books/".$fileName."/".$index.".txt"; if(file_exists($fileName)){
$myfile = fopen($fileName, "r") or die("Unable to open file!");
$content = fread($myfile,filesize($fileName));
fclose($myfile); $content = str_replace("\r\n","<br>", $content);
$rsp = array("code" => 200, "msg" => "读取内容成功", "con" => $content);
$rsp = json_encode($rsp); echo $rsp;
exit();
}else{
// 文件不存在
$rsp = array("code" => 404, "msg" => "文件不存在");
$rsp = json_encode($rsp);
echo $rsp;
exit();
}
}else{
echo "error request, please check your request content";
exit();
} ?>

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