如何用php解析java属性文件?

时间:2015-07-30 11:02:18

标签: php

您好我以正确的方式解析我的java属性文件有一点问题。我需要一个数组用于每个id,因为我想将所有内容存储在我的SQL数据库中。我告诉你我到目前为止所取得的成就。也许你知道如何以正确的方式做到这一点:

01.meta的内容:

12345.fileName=myfile.jpg
12345.path=files/myfile.jpg
12346.fileName=myfile2.jpg
12346.path=files/myfile2.jpg

我的php:

<?php                   
    $file_path = "files/01.meta";
    $lines = explode("\n", trim(file_get_contents($file_path)));
    $properties = array();

    foreach ($lines as $line) {
        $line = trim($line);

        if (!$line || substr($line, 0, 1) == '#') // skip empty lines and comments
        continue;

        if (false !== ($pos = strpos($line, '='))) {
            $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
        }
    }

    print_r($properties);
?>

结果:

Array ( [12345.fileName] => myfile.jpg [12345.path] => files/myfile.jpg [12346.fileName] => myfile2.jpg [12346.path] => files/myfile2.jpg )

我需要什么:

Array ( [id] => 12345 [fileName] => myfile.jpg [path] => files/myfile.jpg )
Array ( [id] => 12346 [fileName] => myfile2.jpg [path] => files/myfile2.jpg )

3 个答案:

答案 0 :(得分:1)

尝试这种方法:

$file_path = "files/01.meta";
$linesArray = file($file_path);    // $linesArray is an array
$properties = array();

$helper = array();

foreach ($linesArray AS $line) {
    if (false !== ($pos = strpos($line, '='))) {
        $prop=array();    // create array ro receive one line 
        $prop[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));

        $lineContArray = explode("=", $line);
        $identArray = explode(".", $lineContArray[0]);

        $ident = $identArray[0];
        $type = $identArray[1];     
        $value = $lineContArray[1];

        if (isset($helper['id']) && $ident != $helper['id'])  {
            $properties[] = $helper;
            unset($helper);
        }
        $helper['id'] = $ident;
        $helper[$type]= $value;
    }
}
if (!empty($helper)) {
    $properties[] = $helper;
}

答案 1 :(得分:1)

sort($lines);    
$properties = array();
for($i = 0; $i < count($lines); $i+=2) {
    $properties[] = array(
        "id" => current(explode(".",$lines[$i])),
        "filename" => next(explode("=",$lines[$i])),
        "path" => next(explode("=",$lines[$i+1]))
    );
}

printf("<pre>%s</pre>", print_r($properties,true));

收率:

Array
(
    [0] => Array
        (
            [id] => 12345
            [filename] => myfile.jpg
            [path] => files/myfile.jpg
        )

    [1] => Array
        (
            [id] => 12346
            [filename] => myfile2.jpg
            [path] => files/myfile2.jpg
        )

)

答案 2 :(得分:1)

比我上一个更优雅的方法如下:

$file_path = "files/01.meta";
$linesArray = file($file_path);    // $linesArray is an array   
$properties = array();

foreach ($linesArray AS $line) {
    if (false !== ($pos = strpos($line, '='))) {
        $prop=array();    // create array ro receive one line 
        $prop[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
        $lineContArray = explode("=", $line);
        $identArray = explode(".", $lineContArray[0]);

        $ident = $identArray[0];
        $type = $identArray[1];     

        $value = $lineContArray[1];

        $found = 0;
        for ($i=0; $i<count($properties); $i++) {
            if ($properties[$i]['id'] == $ident) {
                $properties[$i][$type]= $value;
                $found=1;
                break;
            }
        }
        if ($found == 0) {
            $properties[] = array('id' => $ident, $type => $value);
        }
    }
}

这个优点是文件中的元素可以按任意顺序排列。