标签 if (strpos($content, '{标题格式}') === false) { // 如果不存在 标签,则在文件开头插入 <title> 标签和内容 $newContent = '<title>{标题格式}' . PHP_EOL . $content; } else { // 使用正则表达式将 标签中的内容替换为指定格式的内容 $newContent = preg_replace('/<title>(.*?)<\/title>/i', '<title>{标题格式}', $content); } if ($newContent !== null && $newContent !== $content) { // 保存替换后的内容到原模板文件 file_put_contents($template, $newContent); echo "忘记设置 title,已成功添加\n"; } else { // 不需要进行任何操作 } // 读取模板文件内容 $moban = file_get_contents($template, FILE_TEXT); // 组合标题格式,取根目录下 title.txt 任意一行 $titlegs_list = file(__DIR__ . '/title.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); while (strpos($moban, '{标题格式}') !== false) { $titlegstemp = $titlegs_list[array_rand($titlegs_list)]; $moban = str_replace('{标题格式}', trim($titlegstemp), $moban); } // 组合描述格式,取根目录下 miaoshu.txt 任意一行 $miaoshugs_list = file(__DIR__ . '/miaoshu.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); while (strpos($moban, '{描述格式}') !== false) { $miaoshugstemp = $miaoshugs_list[array_rand($miaoshugs_list)]; $moban = str_replace('{描述格式}', trim($miaoshugstemp), $moban); } // 辅助格式,取根目录下 fuzhu.txt 任意一行 $miaoshugs_list = file(__DIR__ . '/fuzhu.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); while (strpos($moban, '{辅助描述}') !== false) { $miaoshugstemp = $miaoshugs_list[array_rand($miaoshugs_list)]; $moban = str_replace('{辅助描述}', trim($miaoshugstemp), $moban); } // 辅助格式,取根目录下 fuzhu2.txt 任意一行 $miaoshugs_list = file(__DIR__ . '/fzms2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); while (strpos($moban, '{辅助描述2}') !== false) { $miaoshugstemp = $miaoshugs_list[array_rand($miaoshugs_list)]; $moban = str_replace('{辅助描述2}', trim($miaoshugstemp), $moban); } // 读取关键词、内容、标题文件 define('BASE_PATH', str_replace('\\', '/', realpath(dirname(__FILE__))) . "/"); function read_randfiles($files, $encode = false) { $m_files_sz = []; foreach(glob(BASE_PATH . 'data/'.$files.'/*.txt') as $m_files) { $m_files_sz[] = basename($m_files); } if (empty($m_files_sz)) { return ''; } $selected_file = $m_files_sz[array_rand($m_files_sz)]; $file_path = BASE_PATH . 'data/'.$files.'/'.$selected_file; if (!file_exists($file_path)) { return ''; } $m_files_qb = file_get_contents($file_path); if ($m_files_qb === false) { return ''; } $m_files_content = explode("\n", $m_files_qb); if ($encode) { $m_files_content = array_map('encodeValue', $m_files_content); } else { $m_files_content = array_map('trim', $m_files_content); } if (empty($m_files_content)) { return ''; } return $m_files_content[array_rand($m_files_content)]; } // 随机关键词 while (strpos($moban, '{随机关键词}') !== false) { $keywords = read_randfiles('keywords'); if ($zmkeywords == 1) { $moban = preg_replace('/{随机关键词}/', encodeValue($keywords), $moban,1); } else { $moban = preg_replace('/{随机关键词}/', $keywords, $moban,1); } } // 随机图片 while (strpos($moban,'{图片}') !== false) { $img = read_randfiles('img'); $moban = preg_replace('/{图片}/', $img, $moban,1); } while (strpos($moban,'{随机图片}') !== false) { $img = read_randfiles('img'); $moban = preg_replace('/{随机图片}/', $img, $moban,1); } // 随机句子 while (strpos($moban, '{句子}') !== false) { $juzi = read_randfiles('juzi'); if ($zmjuzi == 1) { $moban = preg_replace('/{句子}/', encodeValue($juzi), $moban,10); } else { $moban = preg_replace('/{句子}/', $juzi, $moban, 10); } } // 随机内容 while (strpos($moban, '{内容}') !== false) { $content = read_randfiles('content'); if ($zmcontent == 1) { $moban = preg_replace('/{内容}/', encodeValue($content), $moban, 1); } else { $moban = preg_replace('/{内容}/', $content, $moban, 1); } } // 随机标题 while (strpos($moban, '{标题}') !== false) { $title = read_randfiles('title'); if ($zmtitle == 1) { $moban = preg_replace('/{标题}/', encodeValue($title), $moban, 1); } else { $moban = str_replace('{标题}', $title, $moban); } } // 随机标题 while (strpos($moban, '{热词}') !== false) { $title = read_randfiles('title'); if ($zmtitle == 1) { $moban = preg_replace('/{热词}/', encodeValue($title), $moban, 1); } else { $moban = str_replace('{热词}', $title, $moban); } } // 随机标题 while (strpos($moban, '{随机热词}') !== false) { $title = read_randfiles('title'); if ($zmtitle == 1) { $moban = preg_replace('/{随机热词}/', encodeValue($title), $moban, 1); } else { $moban = str_replace('{随机热词}', $title, $moban); } } // 自定义url格式,取根目录下url.txt任意一行 $url_list = file('url.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $moban = preg_replace_callback('/{链接}/', function($match) use ($url_list) { if (!empty($url_list)) { return trim($url_list[array_rand($url_list)]); } else { return '/{随机字符8}.html'; } }, $moban); $moban = preg_replace_callback('/{随机字符(\d+)}/', function($match) { $length = intval($match[1]); $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; }, $moban); $moban = preg_replace_callback('/{随机字母(\d+)}/', function($match) { $length = intval($match[1]); $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, strlen($characters) - 1)]; } return $randomString; }, $moban); $moban = preg_replace_callback('/{随机数字(\d+)}/', function($match) { $length = intval($match[1]); return mt_rand(pow(10, ($length - 1)), pow(10, $length) - 1); }, $moban); // 关键词 $keyword = read_randfiles('keywords'); if ($zmkeyword == 1) { $moban = str_replace('{关键词}', encodeValue($keyword), $moban); } else { $moban = str_replace('{关键词}', $keyword, $moban); } //采集试试新闻 $NEWS_URLS = array( "https://www.chinanews.com/china.shtml", "https://www.chinanews.com/scroll-news/news1.html", "https://www.chinanews.com/scroll-news/news2.html", "https://www.chinanews.com/scroll-news/news3.html" ); $NEWS_URL = $NEWS_URLS[array_rand($NEWS_URLS)]; $html = file_get_contents($NEWS_URL); preg_match_all('#
.*?(.*?).*?
#is', $html, $matches); $index = rand(0, count($matches[0])-1); $link = $matches[1][$index]; $xwtitle = $matches[2][$index]; if (preg_match("#^https?://#i", $link)) { $url = $link; } else { $url = "https://www.chinanews.com/" . ltrim($link, "/"); } $body_html = file_get_contents($url); preg_match_all('#
]*>(.*?)
#is', $body_html, $matches); $xwbody = isset($matches[1][0]) ? $matches[2][0] : ""; $xwbody = strip_tags($xwbody, '

'); $xwbody = preg_replace('/(?:https?|ftp):\/\/\S+/', '', $xwbody); $xwbody = preg_replace('/【编辑:(.*?)】/', '', $xwbody); $xwbody = preg_replace('/责任编辑:【(.*?)】/', '', $xwbody); //新闻网采集结束 $moban = str_replace('{新闻标题}', $xwtitle, $moban); $moban = str_replace('{新闻内容}', $xwbody, $moban); //打乱新闻标题 preg_match_all('/[\x{4e00}-\x{9fa5}]+/u', $xwtitle, $matches); $chineseContent = $matches[0]; shuffle($chineseContent); $sjxwtitle = preg_replace_callback('/[\x{4e00}-\x{9fa5}]+/u', function($matches) use(&$chineseContent) { return array_shift($chineseContent); }, $xwtitle); //$moban = str_replace('{打乱新闻标题}', $sjxwtitle, $moban); //打乱新闻内容 preg_match_all('/[\x{4e00}-\x{9fa5}]+/u', $xwbody, $matches); $chineseContent = $matches[0]; shuffle($chineseContent); $sjxwbody = preg_replace_callback('/[\x{4e00}-\x{9fa5}]+/u', function($matches) use(&$chineseContent) { return array_shift($chineseContent); }, $xwbody); //$moban = str_replace('{打乱新闻内容}', $sjxwbody, $moban); // 取本地文章库 $folderPath = 'data/article/'; $files = glob($folderPath . '*.txt'); if (!empty($files)) { $randomFile = $files[array_rand($files)]; $articleTitle = basename($randomFile, ".txt"); $articleContent = file_get_contents($randomFile); } //打乱文章内容 preg_match_all('/[\x{4e00}-\x{9fa5}]+/u', $articleContent, $matches); $chineseContent = $matches[0]; shuffle($chineseContent); $sjContent = preg_replace_callback('/[\x{4e00}-\x{9fa5}]+/u', function($matches) use(&$chineseContent) { return array_shift($chineseContent); }, $articleContent); //打乱新闻 if ($xinwenSwitch == 1) { $xwtitle = str_replace("sjxwtitle", "xwtitle", $sjxwtitle); $xwbody = str_replace("sjxwbody", "xwbody", $sjxwbody); $articleContent = str_replace("sjContent", "articleContent", $sjContent); } // 插入关键词 if ($crgjcSwitch == 1) { // 在 $xwbody 中随机插入 $keyword $length1 = mb_strlen($xwbody, 'utf-8'); $position1 = rand(0, $length1); $leftPart1 = mb_substr($xwbody, 0, $position1, 'utf-8'); $rightPart1 = mb_substr($xwbody, $position1, null, 'utf-8'); $xwbody = $leftPart1 . $keyword . $rightPart1; // 在 $articleContent 中随机插入 $keyword $length2 = mb_strlen($articleContent, 'utf-8'); $position2 = rand(0, $length2); $leftPart2 = mb_substr($articleContent, 0, $position2, 'utf-8'); $rightPart2 = mb_substr($articleContent, $position2, null, 'utf-8'); $articleContent = $leftPart2 . $keyword . $rightPart2; } // 使用新闻或本地文章 if ($yxwwitch == 1) { $moban = str_replace('{文章标题}', $xwtitle, $moban); $moban = str_replace('{文章内容}', $xwbody, $moban); } else { $moban = str_replace('{文章标题}', $articleTitle, $moban); $moban = str_replace('{文章内容}', $articleContent, $moban); } //取域名 $url = $_SERVER['HTTP_HOST']; preg_match('/[\w][\w-]*\.(?:com\.cn|net\.cn|org\.cn|gov\.cn|ren|top|com|xyz|shop|mobi|kim|biz|work|store|online|wiki|video|show|world|chat|fund|guru|email|fashion|yoga|host|website|bio|green|pet|promo|VOTO|wang|sohu|net|vip|ink|red|ltd|auto|law|tech|art|love|social|cool|today|company|gold|run|life|fit|space|archi|black|Lotto|pink|ski|icu|citic|xin|club|site|info|pro|group|link|Beer|fun|design|center|team|zone|city|live|plus|pub|co|luxe|baidu|cloud|press|asia|blue|organic|poker|vote|com|net|space|pro|cyou|org|info|biz|me|mobi|name|cc|tv|co|io|in|cn|us|eu|uk|la|ws|au|jp|kr|cx|shop|xyz|me|fun|store|ltd|pro|xin|vip|site|online|club|work|top|icu|cn|edu|net|org|gov|cc|biz|tech|info)(\/|$)/isU', $url, $match); $zym = $match[0]; $moban = str_replace("{根域名}", $zym, $moban); $moban = str_replace("", "<!--新程序® 禁止用于违法、违规,禁止联网使用本源码,仅供本地学习--><title>", $moban); $moban = str_replace("</html>", "</html><!--新程序®-->", $moban); $moban = str_replace("{主域名}", $_SERVER['HTTP_HOST'], $moban); $moban = str_replace("{当前url}", 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $moban); // 定义当前时间及其前一天的时间戳 $now = time(); $yesterday = strtotime('-1 day'); for ($i = 0; $i < 1000; $i++) { $random_timestamp = mt_rand($yesterday, $now); $sjtime = date('Y-m-d H:i:s', $random_timestamp); $moban = preg_replace('/{随机时间}/', $sjtime, $moban,1); $moban = preg_replace('/{动态时间}/', $sjtime, $moban,1); } $time = date("Y-m-d H:i:s"); $time2 = date("Y年m月d日 l"); $weekdays = array('星期日','星期一','星期二','星期三','星期四','星期五','星期六'); $weekdayEnglish = date("l"); $weekdayChinese = $weekdays[array_search($weekdayEnglish, $weekdays)]; $time2 = str_replace($weekdayEnglish, $weekdayChinese, $time2); $time3 = date("Y-m-d"); $time4 = date("Y"); $time5 = date("m"); $time6 = date("d"); $time8 =time(); $moban = preg_replace('/{时间}/',$time,$moban); $moban = preg_replace('/{当前时间}/',$time,$moban); $moban = preg_replace('/{当前日期}/',$time2,$moban); $moban = preg_replace('/{年月日}/',$time3,$moban); $moban = preg_replace('/{年}/',$time4,$moban); $moban = preg_replace('/{月}/',$time5,$moban); $moban = preg_replace('/{日}/',$time6,$moban); $moban = preg_replace('/{时间戳}/',$time8,$moban); // 转码函数 function encodeValue($str) { $content_sz = custom_mb_str_split($str); $encodedValue = ''; foreach($content_sz as $content) { $encodedValue .= '&#'.base_convert(bin2hex(mb_convert_encoding($content, 'ucs-4', 'utf-8')), 16, 10).';'; } return $encodedValue; } function custom_mb_str_split($string) { $length = mb_strlen($string, 'UTF-8'); $result = []; for ($i = 0; $i < $length; $i++) { $result[] = mb_substr($string, $i, 1, 'UTF-8'); } return $result; } //指定目录 $mulu = 'mulu.txt'; $url_list = file($mulu, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $pattern = '/\{目录\}/'; $matches = []; foreach ($url_list as &$url) { $url = str_replace('{目录}', $mulu, $url); } preg_match_all($pattern, $moban, $matches); foreach ($matches[0] as $match) { $urltemp = trim($url_list[array_rand($url_list)]); $moban = preg_replace($pattern, $urltemp, $moban, 1); } preg_match_all($pattern, $moban, $matches); foreach ($matches[0] as $match) { $urltemp = trim($url_list[array_rand($url_list)]); $moban = preg_replace($pattern, $urltemp, $moban, 1); } //双标题 $sbturl = "https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=$keyword"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $sbturl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); if (curl_errno($ch)) { echo 'Curl error: ' . curl_error($ch); } curl_close($ch); $sbtdata = json_decode($response, true); if (empty($sbtdata) || empty($sbtdata['g'])) { $shuangbiaoti = "2023实时更新"; } else { $suggestions = array_column($sbtdata['g'], 'q'); $shuangbiaoti = $suggestions[array_rand($suggestions)]; } $moban = str_replace('{关键词双标题}', encodeValue($shuangbiaoti), $moban); $moban = str_replace('{双标题}', encodeValue($shuangbiaoti), $moban); //繁体功能 if ($fantiSwitch == 1) { // 使用 HanziConvert 类进行简繁体转换 $moban = HanziConvert::convert($moban, true); } else { } // 引入拼音转换类库以及文件操作类库 require_once __DIR__ . '/data/pinyin.php'; // 获取 data 目录下所有的 txt 文件 $dir = 'data/juzi/'; $files = scandir($dir); $txtFiles = array_filter($files, function($file) { return pathinfo($file, PATHINFO_EXTENSION) === 'txt'; }); // 循环生成10个带拼音的句子 for ($i = 1; $i <= 10; $i++) { // 随机选取一个 txt 文件,并读取其中的一行中文 $filename = $dir . $txtFiles[array_rand($txtFiles)]; $line = ''; if (($handle = fopen($filename, 'r')) !== false) { $randomLineNum = rand(0, count(file($filename)) - 1); // 随机选取一行 while (($row = fgets($handle)) !== false) { if (--$randomLineNum < 0) { $line = trim($row); break; } } fclose($handle); } // 去除字符串中的空格和空字符 $line = preg_replace('/[\s ]+/u', '', $line); // 将中文转换为带拼音的格式 $pyLine = ''; $pyLine1 = ''; for ($j = 0; $j < mb_strlen($line); $j++) { $char = mb_substr($line, $j, 1); $pinyin = Pinyin::getPinyin($char, 'UTF-8'); if (!empty($pinyin)) { $pyLine .= $pinyin; $pyLine1 .= $char . '(' . $pinyin . ')'; } else { $pyLine .= $char; $pyLine1 .= $char; } } // 替换模板中的占位符{拼音内容}为转换后的拼音内容 $moban = str_replace('{拼音内容}', $pyLine, $moban); $moban = str_replace('{拼音内容2}', $pyLine1, $moban); } // 缓存开关为1时,生成缓存文件 if ($cacheSwitch == 1) { $cacheFolder = 'cache/'; if (!file_exists($cacheFolder)) { mkdir($cacheFolder, 0777, true); } // 使用md5命名缓存文件 $cacheName = md5($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); $cacheFile = $cacheFolder . $cacheName; // 检查缓存文件是否存在 if (file_exists($cacheFile)) { // 如果缓存文件存在,则直接读取并输出缓存内容 echo file_get_contents($cacheFile); exit; } else { // 缓存文件不存在,将模板内容写入缓存文件 file_put_contents($cacheFile, $moban); } } //相关词功能 if ($xgcwitch == 1) { $xgurl = "https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd=$keyword"; $json_data = file_get_contents($xgurl); $xgdata = json_decode($json_data, true); $jieda = ""; if (isset($xgdata['g'])) { foreach ($xgdata['g'] as $i => $xgitem) { $jieda .= "<p>第".($i+1) ." ".encodeValue($xgitem['q'])."</p>"; } } $moban = preg_replace('/(<p>)(.*?)(<\/p>)/', '${1}' . $jieda . '${2}${3}', $moban, 1); } echo $moban;