简体   繁体   English

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

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

I want to add words into a txt document into sentences from the selected list in a text document with php.我想用php将文本文件中的选定列表中的单词添加到txt文档中。 For example;例如; lorem.txt is the text file like this: lorem.txt 是这样的文本文件:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. 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 exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. 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 hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Duis autem vel eum iriure dolor in vulputate velit esse Molestie consequat

and list.txt is the wordlist like this:和 list.txt 是这样的词表:

hello你好
stackoverflow堆栈溢出
you are great你很棒

and final text will be like:最终文本将如下所示:

Lorem ipsum dolor hello sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet stackoverflow dolore magna aliquam erat volutpat. 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 exerci tation ullamcorper suscipit you are great lobortis nisl ut aliquip ex ea commodo consequat. 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 dolor in hello hendrerit in vulputate velit esse stackoverflow molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis you are great dolore te feugait nulla facilisi. 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。

Thank for help.感谢您的帮助。

  • you can use file_get_contents to read the file您可以使用file_get_contents来读取文件
  • explode(" ", $content) to split the contents of the file on a space so you get an array with words. explode(" ", $content)将文件的内容分割成一个空格,这样你就可以得到一个包含单词的数组。
  • then you can insert the new words in random places in that array using array_splice .然后您可以使用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