php實作小說閱讀功能的方法:1、解析TXT文件,產生章節內容;2、取得章節列表,實作取得章節內容介面即可。
本文操作環境:Windows7系統、PHP7.1版,DELL G3電腦
php怎麼實現小說閱讀功能?
php實作TXT小說章節解析、小說章節線上閱讀
#要實作TXT文本章節的解析,大概思路是在每個章節加入了特定的字符,然後根據字符的起始位置讀取章節。這裡我寫了一個小說閱讀項目,也是依照這個思路進行。
實現步驟:
一. 解析TXT文件,產生章節內容
1. 編輯TXT文件,在每個章節名稱中加入我自己定義的一個字串,用以識別。
2.辨識章節,取得章節清單
二.取得章節列表,實現獲取章節內容介面 1.章節列表介面 介面# 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(); } ?>推薦學習:《
PHP影片教學
》以上是php怎麼實現小說閱讀功能的詳細內容。更多資訊請關注PHP中文網其他相關文章!