1

I'm trying that my php script reads a configuration file. I'm able to open the file and load each line of it to each array position with the file command. However, now I need to analyse each line but could not find a way to read each field, like an awk equivelent.

The configuration file is like this:

GPIO; Direction; Active_low; Default_value; web_page; Description
1; out; 0; 0; yes; ficheiro1
2; out; 0; 0; yes; ficheiro2

My php script is this:

$conf=file('/etc/gpio.conf', FILE_SKIP_EMPTY_LINES);
for ($i=1; $i <3; $i++)
{
 echo" conf $i: $conf[$i] <br />";
 $GPIO=$1stfield_of_conf[i];
 $Direction=$2ndfield_of_conf[i];
 $Active_low=$3rdfield_of_conf[i];
}

What I would need would be something like awk '{print $1}', but capable of read a PHP array...

Any idea/suggestion?

4

3 に答える 3

2

さあ、これを試してみてください:

$conf=file('/etc/gpio.conf', FILE_SKIP_EMPTY_LINES);
    for ($i=1; $i <3; $i++)
  {
echo" conf $i: $conf[$i] <br />";
    $temp_array=explode(";", $conf[$i]);
echo "GPIO: $temp_array[0]";
echo "web: $temp_array[4] <br />";
  }

乾杯

于 2013-05-30T02:30:24.733 に答える
0

また、実際に配列でそれらを必要としない場合は、list() をexplode() と組み合わせて使用​​できます。値がこのループ反復内にのみ存在する場合、$temp_array[3] ではなく、意味のある名前を付けた方がコードが読みやすくなることがあります。

$conf = file('/etc/gpio.conf', FILE_SKIP_EMPTY_LINES);

for ( $i = 1; $i < 3; $i++ )
{
    echo" conf $i: $conf[$i] ";

    list(
        $GPIO, $Direction, $Active_low, $Default_value, $web_page, $Description
    ) = explode( ";", $conf[$i] );

    echo "GPIO: $GPIO\n";
    echo "Direction: $Direction\n";
    echo "web: $web_page\n";
}
于 2014-02-06T22:24:51.693 に答える