0

さて、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) )

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

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

( itemname 597 "Blue Box")

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

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

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

ありがとう

4

4 に答える 4

2

次の方法は実際にはファイルを「解析」しませんが、特定の問題に対しては機能するはずです...

(注: 未テスト)

与えられた:

$item = 397;

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

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;
于 2011-01-20T21:14:14.390 に答える
1

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

ファイル内の括弧で囲まれた式は、s 式と呼ばれます。s-expression パーサーをグーグルで検索し、それを php に適合させることができます。

于 2011-01-20T20:45:47.523 に答える
1

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

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

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

アイテム配列をシリアル化すると、保存して後でアクセスできる形式になります。

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

次に、ファイルをロードし、その内容をシリアル化解除して、同じ配列にすることができます。serialize 関数と unserialize 関数は、このアプリケーションを直接対象としています。

于 2011-01-20T20:46:14.157 に答える
0

最初のテキスト ファイルには、解析に役立ついくつかの機能があります。キーオンするのに十分な形式と信頼性があるかどうかを判断するのは、あなた次第です。

私は気づきました:

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.
于 2011-01-20T21:14:10.237 に答える