从文本文件中读取复杂的结构并放在数据库中

时间:2012-11-25 14:59:12

标签: php text

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

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'值,需要将它们添加到一行。那么如何获取这些值并将它们变成变量以便我可以使用它们呢?

感谢。

3 个答案:

答案 0 :(得分: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
   }
}

我用你的示例文本对它进行了测试,结果很好: - )

答案 1 :(得分: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});"

答案 2 :(得分: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

希望这有用。