美文网首页PHP
TXT小说分章节阅读 , 上班敲不下去代码的时候可以休息一手

TXT小说分章节阅读 , 上班敲不下去代码的时候可以休息一手

作者: Go狗子 | 来源:发表于2019-11-28 10:45 被阅读0次

    先看效果


    image.png

    PHP代码

        /**
         * [index description]
         * @author GoDogson
         * @DateTime 2019-08-09T11:43:03+0800
         * @return   [type]                   [description]
         */
        public function txt($title = 1) {
            // $url = 'http://www.lu.com/index/txt/title/' . ($title + 1);
            // echo '<a href="' . $url . '">' . $title . '</a>';
            // 打印点东西 , 你懂得
            var_dump(db('users')->order('rand()')->select());
            $file = cache('file');
            // $file = '';
            if (!$file) {
                // 文件目录
                $file = file_get_contents('2.txt');
                // txt加入缓存
                cache('file', $file);
            }
            $file = $file;
            // 获取第几章到第几章
            $title = intval($title);
            $end = $title + 1;
            // 转章节名字 得到 "第一百三十五章"
            $titlestr = '第' . \think\helper\Str::number2chinese($title) . '章';
            $titlenum = stripos($file, $titlestr);
            // 如果不存在则查找 "第135章"
            if (!$titlenum) {
                // 章节名称补0 得到 "第0135章" 此处根据下载的txt小说来
                $title = str_pad($title, 4, "0", STR_PAD_LEFT);
                $titlestr = '第' . $title . '章';
                $titlenum = stripos($file, $titlestr);
            }
            // echo $title;exit();
            $endtitlestr = '第' . \think\helper\Str::number2chinese($end) . '章';
            $endtitlenum = stripos($file, $endtitlestr);
            if (!$endtitlenum) {
                $end = str_pad($end, 4, "0", STR_PAD_LEFT);
                $endtitlestr = '第' . $end . '章';
                $endtitlenum = stripos($file, $endtitlestr);
            }
            echo $titlestr . ' == ' . $endtitlestr . "<br>";
            echo $endtitlenum . '===' . $titlenum . "<br>";
            $num = intval($endtitlenum) - intval($titlenum);
            echo "$num";
            // 截图章节文字
            $str = substr($file, $titlenum, $num);
            $this->assign('str', $str);
            return $this->fetch();
            // $this->redirect('vote/vote/index');
        }
    
        /**
         * 数字转中文
         * [number2chinese description]
         * @author QQ:530411799
         * @DateTime 2019-11-11T16:50:43+0800
         * @param    [type]                   $num [description]
         * @return   [type]                        [description]
         */
        public static function number2chinese($num) {
            if (is_int($num) && $num < 100) {
                $char = array('零', '一', '二', '三', '四', '五', '六', '七', '八', '九');
                $unit = ['', '十', '百', '千', '万'];
                $return = '';
                if ($num < 10) {
                    $return = $char[$num];
                } elseif ($num % 10 == 0) {
                    $firstNum = substr($num, 0, 1);
                    if ($num != 10) {
                        $return .= $char[$firstNum];
                    }
    
                    $return .= $unit[strlen($num) - 1];
                } elseif ($num < 20) {
                    $return = $unit[substr($num, 0, -1)] . $char[substr($num, -1)];
                } else {
                    $numData = str_split($num);
                    $numLength = count($numData) - 1;
                    foreach ($numData as $k => $v) {
                        if ($k == $numLength) {
                            continue;
                        }
    
                        $return .= $char[$v];
                        if ($v != 0) {
                            $return .= $unit[$numLength - $k];
                        }
    
                    }
                    $return .= $char[substr($num, -1)];
                }
                return $return;
            }
        }
    

    HTML代码

    <!DOCTYPE html>
    <html>
    <head>
      <title></title>
    </head>
    <body>
    
    </body>
    <script type="text/javascript">
            // 这里直接打印 一般会报错 这样在F12中点击第一个图的错误就可以小窗看小说了
        console.log("{$str}")
    </script>
    </html>
    

    相关文章

      网友评论

        本文标题:TXT小说分章节阅读 , 上班敲不下去代码的时候可以休息一手

        本文链接:https://www.haomeiwen.com/subject/obbvwctx.html