如何从自定义词典随机取词生成不重复的PHP文章标题?
解决方案:自定义词典生成不重复标题
步骤1:添加自定义词典与标题生成方法
在Submitter类中添加自定义词典属性,以及生成唯一标题的方法:
namespace Submitters; use App; use User; use Lang; class Submitter { use \Purify; use \Prepare; use \Stats; protected $app; protected $sql; protected $raw; protected $type; protected $post; protected $user; protected $notes; protected $output; // 自定义标题词典,可根据业务需求扩展 protected $titleDictionary = [ '实用技巧', '深度解析', '全面指南', '入门教程', '进阶攻略', '鲜为人知的', '高效实用的', '值得收藏的', '最新发布的', '手把手教你' ]; // --- 原有构造函数等代码保持不变 --- /** * 生成唯一标题:随机取词典词 + 分类/标签组合 * @param string $category 文章分类 * @param string $tags 标签字符串(逗号分隔) * @return string 不重复的标题 */ private function generateUniqueTitle($category, $tags) { // 处理标签数组,过滤空值 $tagsArray = array_filter(array_map('trim', explode(',', $tags))); // 随机选择词典中的词/短语 $randomTerm = $this->titleDictionary[array_rand($this->titleDictionary)]; // 构建标题基础片段 $titleParts = []; if (!empty($category)) { $titleParts[] = $category; } $titleParts[] = $randomTerm; // 随机选取1-2个标签加入标题(避免标题过长) if (!empty($tagsArray)) { shuffle($tagsArray); $selectedTags = array_slice($tagsArray, 0, mt_rand(1, min(2, count($tagsArray)))); $titleParts = array_merge($titleParts, $selectedTags); } // 打乱片段顺序提升随机性 shuffle($titleParts); $candidateTitle = implode(' ', $titleParts); // 检查标题是否已存在,最多尝试5次重新生成 $attempts = 0; // 注意:替换`posts`为你的文章表实际名称 while ($this->sql->countRows('posts', 'title=?', $candidateTitle) && $attempts < 5) { $randomTerm = $this->titleDictionary[array_rand($this->titleDictionary)]; shuffle($tagsArray); $selectedTags = array_slice($tagsArray, 0, mt_rand(1, min(2, count($tagsArray)))); $titleParts = []; if (!empty($category)) { $titleParts[] = $category; } $titleParts[] = $randomTerm; $titleParts = array_merge($titleParts, $selectedTags); shuffle($titleParts); $candidateTitle = implode(' ', $titleParts); $attempts++; } // 若5次仍重复,添加随机数字后缀确保唯一性 if ($attempts >= 5) { $candidateTitle .= ' ' . mt_rand(100, 999); } return $candidateTitle; } // --- 原有其他方法保持不变 --- }
步骤2:替换标题生成逻辑
修改prepareData方法中的标题生成代码,替换为调用新的generateUniqueTitle方法:
private function prepareData() { $username = $this->user->get('username', $this->post('nick')); $input = filter_input_array(INPUT_POST, [ // --- 原有input过滤代码保持不变 --- ]); return (object) array_merge($input, [ // --- 原有其他字段保持不变 --- // 替换原有的标题生成逻辑 'title' => $this->isValid('title', $this->generateUniqueTitle($input['category'], $input['tags'])) ]); }
可选优化:从数据库加载词典
如果需要更灵活地管理词典(比如后台添加/修改),可以创建一个title_dictionary表,然后在构造函数中加载词典:
-- 创建词典表示例 CREATE TABLE title_dictionary ( id INT AUTO_INCREMENT PRIMARY KEY, term VARCHAR(100) NOT NULL UNIQUE COMMENT '标题用词/短语' );
然后修改构造函数:
public function __construct(App $app, $type) { $this->app = $app; $this->sql = $app->make('db'); $this->user = $app->make('user'); $this->http = $app->make('http'); $this->note = $app->make('notes'); $this->config = $app->make('config'); $this->web = $app->make('webdata'); $this->submissionDisabled(); User::can('submit.content') ?: App::Redirect(); $this->type = $this->postType($type); // 从数据库加载自定义词典 $this->titleDictionary = $this->sql->fetchColumn('SELECT term FROM title_dictionary'); // 数据库无数据时使用默认词典 if (empty($this->titleDictionary)) { $this->titleDictionary = [ '实用技巧', '深度解析', '全面指南', '入门教程', '进阶攻略' ]; } $this->output = $this->prepareData(); App::createTempdirDir(); $this->execute(); }
内容的提问来源于stack exchange,提问作者Алексей
相关产品推荐
相关产品推荐

