我想在带有php的文本文档中将所选单词中的单词添加到txt文档中。 例如; lorem.txt是这样的文本文件:
Lorem ipsum dolor sit 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。 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。
和list.txt是这样的词表:
您好
计算器
你很棒
和最终文本将如下:
Lorem ipsum dolor 你好坐下来,这是一个不错的选择, 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 你很棒 lobortis nisl ut aliquip ex ea commodo consequat。 Duis autem vel eum iriure 在你好在vulputate velit esse中的hendrerit 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 你很棒 dolore te feugait nulla facilisi。
感谢您的帮助。
答案 0 :(得分:1)
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));