0

此代码可以创建一个新的 .txt 文件,如果该文件尚不存在,它将创建该文件。

<?php
$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);
?>

并且这里的代码将每一行字符串标识为一个标记。

<?php
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well  */
$tok = strtok($string, " \n\t");

while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}
?>

people.txt 看起来像这样

John Smith
John Meyer
John Chase 
John Smith    //i want to transfer this set of string into a new file

我在这里想念什么?

4

2 回答 2

0
$fp=fopen('people.txt','a+');
fputs($fp,"John Smith\r\n");
fclose($fp);

打开并附加文件,不要将其全部读入并附加到后面并将其全部写回。当文件超过一定的大小时,这将是真正低效的。我不确定您如何尝试对其进行标记。在我看来,你可以做一个$names=file('people.txt');并立即将它们放在一个数组中......

于 2010-11-12T14:11:59.110 回答
0
$searchTerm = 'John Smith';
$file = 'people.txt';
$fileFound = 'peopleFound.txt';
$current = file_get_contents($file);
if (strpos($searchTerm, $current) !== false) {
    file_put_contents($fileFound, $searchTerm."\n", FILE_APPEND);
}
于 2010-11-13T08:25:53.097 回答