【发布时间】:2013-06-30 15:29:31
【问题描述】:
假设我有一个这样的文本文件:
Welcome to the text file!
-------------------------
Description1: value1
Description2: value2
Description containing spaces: value containing spaces
Description3: value3
将这些数据存储到文本文件中很容易,如下所示:
$file = 'data/preciousdata.txt';
// The new data to add to the file
$put = $somedescription .": ". $somevalue;
// Write the contents to the file,
file_put_contents($file, $put, FILE_APPEND | LOCK_EX);
每次写信都有不同的描述和价值。
现在我想读取数据,所以你会得到文件:
$myFile = "data/preciousdata.txt";
$lines = file($myFile);//file in to an array
假设我刚刚在我的文本文件中写入了“颜色:蓝色”和“味道:辛辣”。我不知道它们在哪一行,我想检索“颜色:”描述的值。
编辑 我是否应该让 php 通过文件“搜索”,返回包含“描述”的行号,然后将该行放入字符串中并删除“:”的所有内容?
【问题讨论】:
-
类似ready line by line然后删除“:”之前的所有字符?
-
您可以轻松使用我之前的answer 中的正则表达式。
-
使用 JSON。 json_encode 和 json_decode 而不是编写自己的解析器。其他选项是序列化和 XML
标签: php