php逐行检测标题是否包含特定标签

用于设置prompt批量提示词时使用,生成出来的文章标题变量难免会有出错,检测一下自动在补全标签。

<?php
$inputFile = 'biaoti.txt';
$outputFile = 'biaoti_updated.txt';
$lines = file($inputFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$noTagCount = 0;
foreach ($lines as $key => $line) {
    if (strpos($line, '【关键词】') === false) {
        $noTagCount++;
        $words = explode(' ', $line);
        $insertPosition = rand(1, count($words) - 1); 
        array_splice($words, $insertPosition, 0, '【关键词】');
        $lines[$key] = implode(' ', $words);
    }
}
echo "检测出问题标题: $noTagCount\n";
file_put_contents($outputFile, implode("\n", $lines));
?>

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注