質問

そうですね、2 つのテキストファイルを解析する必要があります。1 つは Item.txt という名前、もう 1 つは Message.txt という名前です。これらはゲーム サーバーの設定ファイルであり、Item にはゲーム内の各アイテムの行が含まれ、Message にはアイテム名、説明、サーバー メッセージなどが含まれます。これが理想とは程遠いことはわかっていますが、この仕組みや形式を変更することはできません。

アイデアはItem.txtにあります。この形式の行があります

(item (name 597) (Index 397) (Image "item030") (desc 162) (class general etc) (code 4 9 0 0) (country 0 1 2) (plural 1) (buy 0) (sell 4) )

php変数がある場合 $item これは 397 (インデックス) に等しいため、最初に「名前」 (597) を取得する必要があります。

次に、Message.txt を開いて次の行を見つける必要があります。

( itemname 597 "Blue Box")

次に、「Blue Box」を変数として PHP に返します。

私がやろうとしているのは、項目の名前と項目のインデックスを返すことです。

おそらくこれが非常に基本的なことであることはわかっていますが、何十ものファイル操作チュートリアルを検索しましたが、必要なものがまだ見つからないようです。

ありがとう

役に立ちましたか?

解決

次のメソッドは実際にはファイルを「解析」しませんが、特定の問題のために機能するはずです...

(注:テストされていません)

与えられた:

$item = 397;

Open Item.txt:

$lines = file('Item.txt');

インデックスを検索します $item そして、取得します $name:

$name = '';
foreach($lines as $line){ // iterate lines
    if(strpos($line, '(Index '.$item.')')!==false){
        // Index found
        if(preg_match('#\(name ([^\)]+)\)#i', $line, $match)){
            // name found
            $name = $match[1];
        }
        break;
    }
}
if(empty($name)) die('item not found');

open message.txt:

$lines = file('Message.txt');

探す $name そして、取得します $msg:

$msg = '';
foreach($lines as $line){ // iterate lines
    if(strpos($line, 'itemname '.$name.' "')!==false){
        // name found
        if(preg_match('#"([^"]+)"#', $line, $match)){
            // msg found
            $msg = $match[1];
        }
        break;
    }
}

$msg これで含める必要があります Blue Box:

echo $msg;

他のヒント

「ファイル操作のチュートリアル」について言及しているため、問題が式の解析にあるのか、それともファイル自体の読み取りにあるのかはわかりません。

ファイル内の括弧で囲まれた式は、S 式と呼ばれます。Google で S-expression パーサーを検索し、それを php に適合させるとよいでしょう。

あなたはを調べる必要があります シリアライズ 関数。これにより、PHPをリロードする必要がある場合に簡単に再解釈できる形式でデータをテキストファイルに保存できます。

このデータを配列としてシリアル化し、テキストファイルに保存すると、配列キーでアクセスできます。あなたの例を取りましょう。配列として、説明したデータは次のようになります。

$items[397]['name'] = 'bluebox';

アイテムアレイのシリアル化は、保存して後でアクセスできる形式に配置されます。

$data = serialize($items);
//then save data down to the text files using fopen or your favorite class

その後、ファイルをロードして、コンテンツの内容を確認して、同じ配列になってしまう可能性があります。このアプリケーション用には、シリアル化および非正規化機能が直接意図されています。

最初のテキストファイルには、それを解析するために使用できるいくつかの機能があります。それが十分に形成されているかどうかを判断するのはあなた次第であり、キーオンするのに十分な信頼性があります。

私は気づきました:

1) a record is delimited by a single line break
2) the record is further delimted by a set of parens () 
3) the record is typed using a word (e.g. item)
4) each field is delimited by parens 
5) each field is named and the name is the first 'word' 
6) anything after the first word is data, delimited by spaces
7) data with double quotes are string literals, everything else is a number

方法:

read to the end of line char and store that
strip the opening and closing parens
strip all closing )
split at ( and store in temp array (see: http://www.php.net/manual/en/function.explode.php)
element 0 is the type (e.g. item)
for elements 1-n, split at space and store in temp array.
element 0 in this new array will be the key name, the rest is data
once you have all the data compartmentalized, you can then store it in an associative array or database. The exact structure of the array is difficult for me to envision without actually getting into it.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top