Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php解析java属性文件?_Php - Fatal编程技术网

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

如何使用php解析java属性文件?,php,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

您好,我在正确解析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 ( [id] => 12345 [fileName] => myfile.jpg [path] => files/myfile.jpg )
Array ( [id] => 12346 [fileName] => myfile2.jpg [path] => files/myfile2.jpg )
尝试以下方法:

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

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

$file\u path=“files/01.meta”;
$linesArray=file($file_path);//$linesArray是一个数组
$properties=array();
foreach($linesArray作为$line){
if(false!==($pos=strpos($line,'=')){
$prop=array();//创建数组或接收一行
$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;
对于($i=0;$i$ident,$type=>$value);
}
}
}


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

请查看
parse_ini_file()
:@Typoheads:谢谢,我查看了您发送给我的链接,但我只能找到如何解析等号前面的所有符号请仔细查看注释部分。我认为这对你来说可能是完美的:嗨,谢谢你,我尝试了代码,并使用
print\r($properties)我得到一个空白的白色页面作为结果。对不起,这行有一个错误:
如果(isset($helper['id')&&&$ident!=$currentID){
你想再试一次吗?谢谢,我得到的结果是:
数组([0]=>数组([12345.fileName]=>myfile.jpg)[1]=>数组([12345.path]=>files/myfile.jpg)[2]=>数组([12346.fileName]=>myfile2.jpg][3]=>Array([12346.path]=>files/myfile2.jpg)[4]=>Array([id]=>files/myfile2.jpg))
非常感谢,这看起来几乎完美:
Array([0]=>Array([12345.fileName]=>myfile.jpg)[1]=>Array([12345.path]=>files/myfile.jpg)[2]=>Array([id]=>12345[fileName]=>myfile.jpg)[3]=>Array([12346.fileName]=>myfile2.jpg)[4]=>Array([id]=>12345[path]=>files/myfile.jpg)[5]=>Array([12346.path]=>files/myfile2.jpg)[6]=>Array([id]=>12346[fileName]=>myfile2.jpg)[7]=>Array([id]=>12346[path]=>files/myfile2.jpg))
.hmm…再编辑一次…如果仍然不能按预期工作,我可能需要在发布之前进行测试:-)谢谢,但是如果我的行混淆了顺序,这就不起作用。我的文件看起来总是不同的。你能不能对它们进行排序?你能提供一个例子吗?非常感谢,用户Burki发布了一个我需要的解决方案,好的。我只是想我可以添加一个在我看来更容易阅读的较短的解决方案。它以混合顺序处理行;)非常感谢,我认为您的解决方案也很好谢谢!太好了!我可以再问您一个问题吗?在我的文件末尾,我还想解析这样编写的文件大小:fileSize=1024595340。使用您的代码,我将得到:
Array([0]=>Array([id]=>12345[fileName]=>myfile.jpg[path]=>files/myfile.jpg)[1]=>Array([id]=>12346[fileName]=>myfile2.jpg[path]=>files/myfile2.jpg)[2]=>Array([id]=>fileSize[]=>1024595340))
有可能获得这样的文件大小吗=>1024595340
。实际上,我用上面的代码实现了这一点,但我不知道如何将我的代码与您的代码结合起来。丑陋,但功能:用以下内容替换最后一个条件:if($found==0){if(is_numeric($ident)){$properties[]=array('id'=>$ident,$type=>$value);}否则{$properties[]=array($ident=>$value)}很高兴我能帮上忙:-)哦,我看到解决方案不起作用了,因为我的id不总是一个数字。有时它是这样的0001421DB213022FG12可能我可以这样做:如果($ident==“fileSize”)?
$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;
}
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
        )

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