【问题标题】:php retrieve data from text filephp从文本文件中检索数据
【发布时间】: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


【解决方案1】:

使用explode,您可以创建一个包含“描述”作为键和“值”作为值的数组。

$myFile = "data.txt";
$lines = file($myFile);//file in to an array
var_dump($lines);

unset($lines[0]);
unset($lines[1]); // we do not need these lines.

foreach($lines as $line) 
{
    $var = explode(':', $line, 2);
    $arr[$var[0]] = $var[1];
}

print_r($arr);

【讨论】:

  • 你也可以这样修剪:$arr[$var[0]] = trim($var[1]);
  • @AurelioDeRosa 这是一个很好的建议。现在他可以为所欲为。 $var[0] 是“Description1”、“Descrption2”(等等...),而 $var[1] 保存该值。 :)
  • 使用explode的limit参数($var = explode(':', $line, 2);)
【解决方案2】:

我不知道您以后想如何使用这些值。您可以将数据加载到关联数组中,其中描述是键:

// iterate through array
foreach($lines as $row) {
    // check if the format of this row is correct
    if(preg_match('/^([^:]*): (.*)$/', $row, $matches)) {
        // get matched data - key and value
        $data[$matches[1]] = $matches[2];
    }
}
var_dump($data);

请注意,此代码允许您获取带有冒号的值。

如果您不确定描述是否唯一,您可以将值存储为数组:

// iterate through array
foreach($lines as $row) {
    // check if the format of this row is correct
    if(preg_match('/^([^:]*): (.*)$/', $row, $matches)) {
        // get matched data - key and value
        $data[$matches[1]][] = $matches[2];
    }
}
var_dump($data);

这可以防止覆盖之前解析的数据。

【讨论】:

    【解决方案3】:

    由于 $lines 是一个数组,您应该循环它以查找“:”并将该行分成两部分:描述和值。

    然后:

    <?php
    $variables = array();
    unset($lines[0]); //Description line
    unset($lines[1]); //-------
    
    foreach ($lines as $line) {
        $tempArray = explode( ": ", $line ); //Note the space after the ":", because
                                               values are stored with this format.
        $variables[$tempArray[0]] = $tempArray[1];
    }
    ?>
    

    变量中有一个数组,其键是描述,值是您的值。

    【讨论】:

      【解决方案4】:

      您可以使用正则表达式拆分行并检索“:”之后的部分,或者只需使用explode命令返回一个由每个“:”拆分的字符串数组,例如:

      foreach ($lines as $line_num => $line) {
          $split_line = explode(":", $line);
          // echo the sencond part of thr string after the first ":"
          echo($split_line[1])
          // also remove spaces arounfd your string
          echo(trim($split_line[1]));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-08
        • 2015-02-19
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-11
        相关资源
        最近更新 更多