<?php // 获取目标页面的HTML内容 function getHTML($url) { $opts = [ "http" => [ "header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" ] ]; $context = stream_context_create($opts); // 添加错误处理,以防file_get_contents失败 $html = @file_get_contents($url, false, $context); if ($html === false) { die("Error fetching content from $url"); } return $html; } // 过滤掉CSS和JS function stripScriptsAndStyles($html) { // 使用正则表达式移除<script>和<style>标签及其内容 $clean_html = preg_replace('/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/is', '', $html); $clean_html = preg_replace('/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/is', '', $clean_html); $clean_html = preg_replace("/\r?\n|\r/", '', $clean_html); return $clean_html; } // 分页函数 function paginate($text, $charsPerPage = 2000) { $pages = []; $totalLength = mb_strlen($text); for ($i = 0; $i < $totalLength; $i += $charsPerPage) { $pages[] = mb_substr($text, $i, $charsPerPage); } return $pages; } // 目标URL(这里可以改为从GET参数获取) if(isset($_GET['bookid'])){ $bookid = str_replace('%2F', '/', rawurlencode($_GET['bookid'])); }else{ $bookid = '/book/1.html';//伪静态。但不包含域名网址 } //$bookid = isset($_GET['bookid']); $url = "https://www.a.com/" . $bookid; //目标页面的网址 // 获取并处理HTML内容 $html = getHTML($url); $clean_html = stripScriptsAndStyles($html); // 创建分页 $pages = paginate($clean_html); $totalPages = count($pages); // 获取当前请求的页面 $currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1; if ($currentPage < 1) { $currentPage = 1; } elseif ($currentPage > $totalPages) { $currentPage = $totalPages; } // 显示当前页面的内容 if ($currentPage <= $totalPages) { echo '<div class="page-content">'; echo $pages[$currentPage - 1]; echo '</div>'; } // 显示分页链接 echo '<div class="pagination">'; for ($i = 1; $i <= $totalPages; $i++) { // 添加bookid到分页链接中 echo '<a href="?bookid=' . str_replace('%2F', '/', rawurldecode($bookid)) . '&page=' . $i . '">' . $i . ' 章</a> '; } echo '</div>'; ?>
原创文章,作者:admin,如若转载,请注明出处:http://baobide.com/810.html