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);

ファイルを開いて追加します。すべてを読み取って後ろに追加し、すべて書き戻すのではありません。ファイルが実際に非効率になる特定のサイズを超えると。どのようにトークン化しようとしているのかわかりません。私には、あなたは単に a$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 に答える