使用 PHP 解析 s 表达式

发布于 2024-10-12 23:00:49 字数 609 浏览 8 评论 0原文

好吧,我需要解析 2 个文本文件。 1 个名为 Item.txt 和一个名为 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) (复数 1) (买 0) (卖 4) )

如果我的 php 变量 $item 等于 397(索引),我需要首先获取“名称”(597)。

然后我需要打开 Message.txt 并找到这一行

( itemname 597 "Blue Box")

然后将“Blue Box”作为变量返回给 PHP。

我想做的是返回项目的名称和项目的索引。

我知道这可能是非常基本的东西,但我已经搜索了数十个文件操作教程,但似乎仍然找不到我需要的东西。

谢谢

Well, I need to parse 2 textfiles. 1 named Item.txt and one named Message.txt They are configuration files for a game server, Item contains a line for each item in the game, and Message has Item names, descriptions, server messages etc. I know this is far less than ideal, but I can't change the way this works, or the format.

The idea is in Item.txt I have lines in this format

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

If I have the php variable $item which is equal to 397 (Index), I need to first get the 'name' (597).

Then I need to open Message.txt and find this line

( itemname 597 "Blue Box")

Then return "Blue Box" to PHP as a variable.

What I'm trying to do is return the item's name with the item's Index.

I know this is probably something really basic, but I've searched though dozens of file operation tutorials and still can't seem to find what I need.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仙气飘飘 2024-10-19 23:00:49

以下方法实际上并不“解析”文件,但它应该适用于您的特定问题...

(注意:未测试)

给定:

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

Following method doesn't actually 'parse' the files, but it should work for your specific problem...

(Note: not tested)

Given:

$item = 397;

open Item.txt:

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

search index $item and get $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');

search $name and get $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 should now contain Blue Box:

echo $msg;
我不在是我 2024-10-19 23:00:49

由于您提到“文件操作教程”,不确定您的问题是解析表达式还是读取文件本身。

文件中的那些括号表达式称为 s 表达式。您可能想在 google 上搜索 s-表达式解析器并将其改编为 php。

Not sure if your problem is with parsing the expressions, or reading files per se since you mention "file operation tutorials".

Those parenthetical expressions in your files are called s-expressions. You may want to google for an s-expression parser and adapt it to php.

鱼忆七猫命九 2024-10-19 23:00:49

您应该查看 serialize 函数,它允许将数据存储到文本文件中采用 PHP 在需要重新加载时可以轻松重新解释的格式。

将此数据序列化为数组并将其保存到文本文件中将允许您通过数组键访问它。让我们以你的例子为例。作为一个数组,您描述的数据看起来像这样:

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

序列化项目数组会将其设置为可以保存并稍后访问的格式。

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

然后,您可以加载该文件并反序列化其内容,最终得到相同的数组。序列化和反序列化函数直接用于此应用程序。

You should look into the serialize function, which allows data to be stored to a textfile in a format that PHP can reinterpret easily when it needs to be reloaded.

Serializing this data as an array and saving it down to the textfiles would allow you to access it by array keys. Let's take your example. As an array, the data you described would look something like this:

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

Serializing the item array would put it in a format that could be saved and later accessed.

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

You could then load the file and unserialize it's contents to end up with the same array. The serialize and unserialize functions are directly intended for this application.

海拔太高太耀眼 2024-10-19 23:00:49

第一个文本文件有几个功能可以用来帮助解析它。由您来决定它是否结构良好且足够可靠以供选择。

我注意到:

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.

the first text file has several features that you can use to help parse it. It is up to you to decide if it is well formed and reliable enough to key on.

I noticed:

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

A method:

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文