繁体   English   中英

如何使用php将特定单词随机添加到目标txt文件?

[英]How to add specific word randomly to targeted txt file using php?

我想用php将文本文件中的选定列表中的单词添加到txt文档中。 例如; lorem.txt 是这样的文本文件:

Lorem ipsum dolor sat amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat。 Ut wisi enim ad minim veniam, quis nostrud exerciation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat。 Duis autem vel eum iriure dolor in vulputate velit esse Molestie consequat

和 list.txt 是这样的词表:

你好
堆栈溢出
你很棒

最终文本将如下所示:

Lorem ipsum dolor hello sat amet,consectetuer adipiscing elit,sed diam nonummy nibh euismod tincidunt ut laoreet stackoverflow dolore magna aliquam erat volutpat。 Ut wisi enim ad minim veniam, quis nostrud exerciation ullamcorper suscipit you are great lobortis nisl ut aliquip ex ea commodo consequat。 DUIS AUTEM VEL EUM iriure悲在招呼hendrerit在vulputate velit埃塞计算器molestie consequat,韦尔ILLUM dolore欧盟feugiat法无在facilisis维罗爱欲等accumsan等iusto奥迪奥dignissim魁blandit praesent luptatum zzril delenit augue DUIS你是伟大的dolore TE feugait法无facilisi。

感谢您的帮助。

  • 您可以使用file_get_contents来读取文件
  • explode(" ", $content)将文件的内容分割成一个空格,这样你就可以得到一个包含单词的数组。
  • 然后您可以使用array_splice在该数组中的随机位置插入新单词。

——

$loremfile = "lorem.txt";
$wordsfile = "list.txt";
$resultfile = "result.txt";

// check if $loremfile exists
if (!file_exists($loremfile)) {
    throw new \InvalidArgumentException("Lorem file does not exists [" . realpath($loremfile) . "]");
}

// check if $wordsfile exists
if (!file_exists($wordsfile)) {
    throw new \InvalidArgumentException("Words file does not exists [" . realpath($wordsfile) . "]");
}

// check if $resultfile exists
if (file_exists($resultfile)) {
    // check if we can overwrite the $resultfile
    if (!is_writable($resultfile)) {
        throw new \InvalidArgumentException("Not enough permissions to overwrite result file [" . realpath($resultfile) . "]");
    }
} else {
    // check if we're allowed to create $resultfile
    if (!is_writable(dirname($resultfile))) {
        throw new \InvalidArgumentException("Not enough permissions to create result file [" . realpath($resultfile) . "]");
    }
}

// read $loremfile and explode on spaces
$lorem = explode(" ", file_get_contents($loremfile));
// array_map(trim) the each word and array_filter to remove empty lines
$lorem = array_filter(array_map('trim', $lorem));

// read $wordsfile and explode on newlines
$words = explode("\n", file_get_contents($wordsfile));
// array_map(trim) the each word and array_filter to remove empty lines
$words = array_filter(array_map('trim', $words));

// print words (for debugging)
var_dump($words);

// loop over words
foreach ($words as $word) {
    // get a random spot in the $lorem array
    $randomIndex = array_rand($lorem);
    // insert the $word into the $randomIndex spot of $lorem
    array_splice($lorem, $randomIndex, 0, $word);
}

// print result (for debugging)
var_dump($lorem, implode(" ", $lorem));

// write result to $resultfile
file_put_contents($resultfile, implode(" ", $lorem));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM