Home  >  Article  >  Backend Development  >  How to implement novel reading function in php

How to implement novel reading function in php

藏色散人
藏色散人Original
2021-09-13 09:05:062577browse

How to implement the novel reading function in php: 1. Parse the TXT file and generate chapter content; 2. Get the chapter list and implement the interface to obtain the chapter content.

How to implement novel reading function in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How does php realize the novel reading function?

php realizes TXT novel chapter analysis and novel chapter online reading

To realize the analysis of TXT text chapters, the general idea is to add specific features to each chapter characters, and then read chapters based on the starting position of the characters. Here I wrote a novel reading project, which is also based on this idea.

Implementation steps:

1. Parse the TXT file and generate chapter content

            1. Edit the TXT file and add a string defined by myself to each chapter name. for identification.

          2. Identify the chapters and obtain the chapter list

            3. Loop through the content of each chapter based on the custom string, and generate a TXT file for each chapter

2. Obtain the chapter list and implement the interface for obtaining chapter content

          1. Chapter list interface

        2. Chapter content interface

Source code:

1. Parse the TXT file and generate chapter content

<?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=&#39;/#titlestart#(.*?)#titleend#/is&#39;;
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 &#39;<br>创建文件夹bookdir成功<br><br>&#39;;
} else {
    echo &#39;<br>需创建的文件夹bookdir已经存在<br><br>&#39;;
}

// 生成目录文件
$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=&#39;/#titlestart#&#39;.$v.&#39;#titleend#(.*?)#titlestart/is&#39;;
    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. Get the chapter list and implement the interface for obtaining chapter content

<?php
header(&#39;Content-Type:application/json&#39;);

$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(&#39;#catalog#&#39;,$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();
}


?>

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to implement novel reading function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn