1

我在文本文件中有以下结构:

class Thing1
{
    pos[]={120.03,121,134.0987};
    heading=-92.049;
    anotherthing=19;
    foo="thing";
    kind="This_item";
    foo=0.293333;
    foo="this thing";
};
class Thing2
{
 ...
}

我想用 PHP 文档阅读我网站上的结构并将其放入数据库中。我需要有 'pos[]'、'heading' 和 'kind' 值并且需要将它们添加到一行中。那么我怎样才能得到这些值并将它们变成一个变量以便我可以使用它们呢?

谢谢。

4

3 回答 3

1

我已经制作了以下解决方案:

$lines = file('your_file.txt',FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach($lines as $ky => $line) {
   $lines[$ky] = explode('=',$line);
   $lines[$ky][0] = trim($lines[$ky][0]);
   if (isset($lines[$ky][1])) {
      $lines[$ky][1] = trim($lines[$ky][1], '";\'{}');
   }
}
foreach($lines as $line){
   if ($line[0] == 'pos[]') {
      echo 'pos[] = ' . $line[1]; //echoing post[] value
   }
   if ($line[0] == 'kind') {
      echo 'kind = ' . $line[1]; //echoing kind value
   }
   if ($line[0] == 'heading') {
      echo 'heading = ' . $line[1]; //echoing heading value
   }
}

我已经用您的示例文本对其进行了测试,并且效果很好:-)

于 2012-11-25T15:45:00.150 回答
0
$FileArray = file("yourfile.txt");
$QueryArray = array();
foreach($FileArray as $Line) {
    if (count($LineArray = explode('=', $Line)) > 1) {
        $QueryArray[$LineArray[0]] = trim($LineArray[1], ";");
    }
}

从那里您可以执行检查并构建查询以根据您的需要将值插入数据库。像这样的东西:

$QueryString = "INSERT INTO {$DbName} (";
foreach($QueryArray as $Key=>$Val) {
    if ($Key == "whatever") {
        $KeyStr .= $Key.', ';
        $ValStr .= $Val.', ';
    }
}
$KeyStr = trim($KeyStr, ", ");
$ValStr = trim($ValStr, ", ");
QueryString .= "{$KeyStr}) VALUES ({$ValStr});"
于 2012-11-25T15:21:17.633 回答
0

我为这个问题写了一个函数:

<?php
$array = parse_my_file("file.txt");

print_r($array);

function parse_my_file($file){
    if(file_exists($file)){
        $data = file_get_contents($file);
        $class = explode("class ", $data);
        unset($class[0]);
        $class_sanitized = array_map(function($v){
            $temp = explode(PHP_EOL . "{" . PHP_EOL, substr($v, 0, -4), 2);
            $temp[1] = array_filter(explode(PHP_EOL, $temp[1]));
            $temp[1] = array_map(function($w){
                $value = explode("=", trim(substr($w, 0,-1), " "));
                return($value);
            }, $temp[1]);
            return($temp);
        }, $class);

        $return_array = array();
        foreach($class_sanitized as $value){
            $return_array[$value[0]] = array_map(function($z){
            if(substr($z[1], 0, 1) == '"' AND substr($z[1], -1) == '"'){
                $z[1] = substr($z[1], 1, -1);
            }
                return(array($z[0] => $z[1]));
            }, $value[1]);
        }
        return $return_array;
    }else{
        return array("Deze bestand bestaat niet");
    }
}
?>

输出是:

Array
(
    [Thing1] => Array
        (
            [0] => Array
                (
                    [pos[]] => {120.03,121,134.0987}
                )

            [1] => Array
                (
                    [heading] => -92.049
                )

            [2] => Array
                (
                    [anotherthing] => 19
                )

            [3] => Array
                (
                    [foo] => thing
                )

            [4] => Array
                (
                    [kind] => This_item
                )

            [5] => Array
                (
                    [foo] => 0.293333
                )

            [6] => Array
                (
                    [foo] => this thing
                )

        )

    [Thing2] => Array
        (
            [0] => Array
                (
                    [pos[]] => {220.03,221,234.0987}
                )

            [1] => Array
                (
                    [heading] => -292.049
                )

            [2] => Array
                (
                    [anotherthing] => 219
                )

            [3] => Array
                (
                    [foo] => thing2
                )

            [4] => Array
                (
                    [kind] => This_item2
                )

            [5] => Array
                (
                    [foo] => 2.293333
                )

            [6] => Array
                (
                    [foo] => this thing2
                )

        )

)

要回显一个值:

echo $array["Thing1"][2]["anotherthing"]; // Result 19

希望这会有所帮助。

于 2012-11-25T17:17:37.633 回答