使用 PHP 解析 s 表达式
好吧,我需要解析 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下方法实际上并不“解析”文件,但它应该适用于您的特定问题...
(注意:未测试)
给定:
打开 Item.txt:
搜索索引
$item
并获取$name
:打开Message.txt:
搜索
$name
并获取$msg
:$msg 现在应该包含
Blue Box
:Following method doesn't actually 'parse' the files, but it should work for your specific problem...
(Note: not tested)
Given:
open Item.txt:
search index
$item
and get$name
:open Message.txt:
search
$name
and get$msg
:$msg
should now containBlue Box
:由于您提到“文件操作教程”,不确定您的问题是解析表达式还是读取文件本身。
文件中的那些括号表达式称为 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.
您应该查看 serialize 函数,它允许将数据存储到文本文件中采用 PHP 在需要重新加载时可以轻松重新解释的格式。
将此数据序列化为数组并将其保存到文本文件中将允许您通过数组键访问它。让我们以你的例子为例。作为一个数组,您描述的数据看起来像这样:
序列化项目数组会将其设置为可以保存并稍后访问的格式。
然后,您可以加载该文件并反序列化其内容,最终得到相同的数组。序列化和反序列化函数直接用于此应用程序。
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:
Serializing the item array would put it in a format that could be saved and later accessed.
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.
第一个文本文件有几个功能可以用来帮助解析它。由您来决定它是否结构良好且足够可靠以供选择。
我注意到:
一种方法:
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:
A method: