PHP を使用してから何年も経ちましたが、私は少し錆びています。大きなファイルを開いて配列に分割し、各値で同様の出現を探す簡単なスクリプトを作成しようとしています。たとえば、ファイルは次のようなもので構成されています。
Chapter 1. The Beginning
Art. 1.1 The story of the apple
Art. 1.2 The story of the banana
Art. 1.3 The story of the pear
Chapter 2. The middle
Art. 1.1 The apple gets eaten
Art. 1.2 The banana gets split
Art. 1.3 Looks like the end for the pear!
Chapter 3. The End
…
2 つの値に文字列 "apple" が含まれていることをスクリプトに自動的に伝え、"Art. 1.1 The Story of the apple" と "Art. 1.1 The apple gets eat" を返し、さらにバナナとナシも同じ。
特定の文字列を配列から検索するつもりはありません。出現回数を数えて、何をどこで返すかだけが必要です。
ファイルを開き、それを配列に分割するスクリプトを既に取得しています。類似のオカレンスを見つける方法がわかりません。
<?php
$file = fopen("./index.txt", "r");
$blah = array();
while (!feof($file)) {
$blah[] = fgets($file);
}
fclose($file);
var_dump($blah);
?>
どんな助けでも大歓迎です。