php php从文本文件中检索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17391958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:49:16  来源:igfitidea点击:

php retrieve data from text file

php

提问by Mdlc

Imagine I have a certain text file like this:

想象一下,我有一个这样的文本文件:

 Welcome to the text file!
 -------------------------
 Description1: value1
 Description2: value2
 Description containing spaces: value containing spaces
 Description3: value3

Storing this data into a text file would be easy, like this:

将此数据存储到文本文件中很容易,如下所示:

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

With a different description and value every time I write to it.

每次我写信时都有不同的描述和价值。

Now I would like to read the data, so you would get the file:

现在我想读取数据,所以你会得到文件:

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

Lets say I just wrote "color: blue" and "taste: spicy" to my text file. I don't know on which lines they are, and I want to retrieve the value of the "color:" description.

假设我刚刚在我的文本文件中写了“颜色:蓝色”和“味道:辣”。我不知道它们在哪一行,我想检索“颜色:”描述的值。

EditShould I let php "search" though the file, return the number of the line that contains the "description", then put the line in a string and remove everything for the ":"?

编辑我应该让php“搜索”文件,返回包含“描述”的行号,然后将该行放入字符串中并删除“:”的所有内容吗?

回答by Ivanka Todorova

With explode you can make an array containg the "description" as a key and the "value" as value.

使用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);

回答by Marcin Krawiec

I don't know how you want to use those values later on. You can load data into an associative array, where the descriptions are keys:

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

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

Please note that this code allows you to fetch values with colons.

请注意,此代码允许您使用冒号获取值。

If you are not sure if the descriptions are unique you can store values as an array:

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

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

This prevents from overwriting data parsed earlier.

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

回答by agustinsantiago

Since $lines is an array, you should loop it looking for the ":" and separating the line in two: description and value.

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

Then:

然后:

<?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];
}
?>

There you have in variables an array, whose keys are the descriptions and values are your values.

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

回答by andre.barata

You could use a regular expression to split the line and retrieve the parte after ":" or just yse the explodecommand which returns an array of the string split by every ":" such as:

您可以使用正则表达式来分割行并在“:”之后检索部分,或者只是使用explodecommand返回一个由每个“:”分割的字符串数组,例如:

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]));
}