", "
'); if ($firstPPosition !== false) { $firstPTagContent = mb_substr($xwbody, $firstPPosition + 3); $insertPosition = $firstPPosition + 3 + 14; $crgjc2 = mb_convert_encoding($crgjc2, 'utf-8'); $xwbody = mb_substr($xwbody, 0, $insertPosition) . $crgjc2 . mb_substr($xwbody, $insertPosition); } $lastPPosition = mb_strrpos($xwbody, '
'); if ($lastPPosition !== false) { $lastPTagContent = mb_substr($xwbody, $lastPPosition + 3); $insertPosition = $lastPPosition + 3 + 14; $crgjc = mb_convert_encoding($crgjc, 'utf-8'); $xwbody = mb_substr($xwbody, 0, $insertPosition) . $crgjc . mb_substr($xwbody, $insertPosition); } } $folderPath = 'data/gpt/'; $files = glob($folderPath . '*.txt'); if (!empty($files)) { $randomFile = $files[array_rand($files)]; $bdtitle = basename($randomFile, ".txt"); $bdbody = file_get_contents($randomFile); } if ($ybdwitch == 1) { // 替换{关键词} $bdtitleArr = explode("\n", $bdtitle); // 将$bdtitle按行分割成数组 $randomKeyword = trim($bdtitleArr[array_rand($bdtitleArr)]); // 随机选择一个关键词 // 获取标题长度 $titleLength = mb_strlen($bdtitle, 'utf-8'); // 在随机位置插入 $randomKeyword $randomPosition = mt_rand(0, $titleLength); $result = mb_substr($bdtitle, 0, $randomPosition, 'utf-8') . $randomKeyword . mb_substr($bdtitle, $randomPosition, null, 'utf-8'); $moban = str_replace('{文章标题}', $result, $moban); $moban = preg_replace_callback('/\{文章标题插词\}/', function () use ($result) { return $result; }, $moban); $moban = str_replace('{文章内容}', $bdbody, $moban); $moban = str_replace('{关键词}', encodeValue($randomKeyword), $moban); // 多次替换{随机关键词} while (strpos($moban, '{随机关键词}') !== false) { $randomFile = $files[array_rand($files)]; $randomKeyword2 = basename($randomFile, ".txt"); $moban = preg_replace('/{随机关键词}/', encodeValue($randomKeyword2), $moban, 1); } } else { $moban = str_replace('{关键词}', $selectedKeyword, $moban); $moban = preg_replace_callback('/\{随机关键词\}/', function () use ($keywordLines) { return trim($keywordLines[array_rand($keywordLines)]); }, $moban); // 将$xwtitle分割为数组 $titleArray = preg_split('//u', $xwtitle, -1, PREG_SPLIT_NO_EMPTY); // 获取数组长度 $titleLength = count($titleArray); // 随机插入位置 $insertPosition = mt_rand(0, $titleLength); // 在指定位置插入$selectedKeyword array_splice($titleArray, $insertPosition, 0, $selectedKeyword); // 拼接标题字符串 $result = implode('', $titleArray); $moban = str_replace('{文章标题}', $xwtitle, $moban); $moban = str_replace('{文章标题插词}', $result, $moban); $moban = str_replace('{新闻标题插词}', $result, $moban); $moban = str_replace('{文章内容}', $xwbody, $moban); } if ($zmtitleSwitch == 1) { $moban = str_replace('{文章标题}', encodeValue($xwtitle), $moban); $moban = str_replace('{文章标题插词}', encodeValue($result), $moban); $moban = str_replace('{新闻标题插词}', encodeValue($result), $moban); } else { $moban = str_replace('{文章标题插词}', $result, $moban); $moban = str_replace('{新闻标题插词}', $result, $moban); $moban = str_replace('{文章内容}', $xwbody, $moban); } // 引入拼音转换类库以及文件操作类库 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); } $shuangpin_files = glob('data/shuangpin.txt'); $randomshuangpinFile = $shuangpin_files[array_rand($shuangpin_files)]; $shuangpinLines = file($randomshuangpinFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); $moban = preg_replace_callback('/\{双拼\}/', function ($matches) use ($shuangpinLines, $zmshuangpinSwitch) { $randomshuangpin = trim($shuangpinLines[array_rand($shuangpinLines)]); return $randomshuangpin; }, $moban); //繁体功能 if ($fantiSwitch == 1) { // 使用 HanziConvert 类进行简繁体转换 $moban = HanziConvert::convert($moban, true); } else { } $context = stream_context_create([ 'http' => [ 'timeout' => 0.5, // 设置超时时间为500毫秒 ], ]); if ($xgcwitch == 1) { $xgurl = "https://www.baidu.com/sugrec?pre=1&p=3&ie=utf-8&json=1&prod=pc&from=pc_web&wd="; if ($ybdwitch == 1) { $xgurl .= urlencode($randomKeyword); } else { $xgurl .= urlencode($selectedKeyword); } $json_data = @file_get_contents($xgurl, false, $context); if ($json_data !== false) { $xgdata = json_decode($json_data, true); $jieda = ""; if (isset($xgdata['g'])) { foreach ($xgdata['g'] as $i => $xgitem) { if ($i < 3) { // 最多显示3组内容 $jieda .= "
第" . ($i + 1) . " ".$randombiaoqing . encodeValue($xgitem['q']) . $randomHotWord . "
"; } } } $moban = preg_replace('/()(.*?)(<\/p>)/', '${1}' . $jieda . '${2}${3}', $moban, 1); } else { $moban = preg_replace('/(
)(.*?)(<\/p>)/', '', $moban, 1); } } // 缓存开关为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); } } echo $moban; ?>