如何从文本文件读取列并将其插入数据库php

时间:2019-04-01 07:44:55

标签: php cakephp

我有一个文本文件,其中有多个行和列。

我想读取每一行和每一列,将它们存储在数组中,并使用cakephp将所有数据保存在数据库中。

下面是我的代码,我写了一些我想实现的行和列读取逻辑。

请帮助我做这个关键的事情。

public function importfile(){
        $fp = 'C:/wamp64/www/jhraut/webroot/uploads/120518SU';
        $handle = fopen($fp, "r");
        if ($handle) {
            while (($line = fgetc($handle)) !== false) {

                $data['GunNo'] = "first 5 characters of $line is GunNo than 1 character is space";
                $data['FatGun'] = "Second 3 characters of $line is FatGun than 1 character is space";
                $data['LoinGun'] = "Third 3 characters of $line is LoinGun than 1 character is space";
                $data['ScaleWt'] = "fourth 5 characters of $line is ScaleWt than 1 character is space";
                $data['Partial'] = "if P or M than 1 character is space";
                $data['TimeofReading'] = "last 8 characters of $line is TimeofReading";
                echo $line;
            }
            $this->Event_program->saveAll($data);
        }        
        fclose($fp);
        exit;
    }

我的文件数据

parti 011 058 145.6 P  06:37:01
00002 016 049 175.8    06:37:08
00003 009 072 150.8    06:37:15
00004 009 053 146.8    06:37:22
00005 011 054 169      06:37:29
00006 009 052 152.4    06:37:37
00007 018 059 194.8    06:37:44
00008 009 060 139.4    06:37:51
parti 008 069 134.8 P  06:37:58
00010 023 054 194.2    06:38:05
miss          197.2    06:38:13
00011 023 052 150      06:38:20
00012 008 059 146.6    06:38:27
00013 010 067 156      06:38:34
00014 013 049 190.8    06:38:41

2 个答案:

答案 0 :(得分:0)

尝试这样的事情:

// set path to file
$file = WWW_ROOT.'uploads/120518SU';
// check if file exists
if (file_exists($file)) {
   // Reads an entire file into an array with file() method
   // and loop array 
   foreach (file($file) as $line) {
      // convert line value to new array
      $arr = explode(' ', $line);

      // do something with your data..
      $data = [
         'GunNo' => $arr[0],
         // ...
      ];

      $entity = $this->EventProgram->newEntity($data);
      $this->EventProgram->save($entity);
   }
}

使用一些测试进行更新:

$line = '00003 009 072 150.8    06:37:15';
$arr = explode(' ', $line);

print_r($arr);

// output

Array
(
    [0] => 00003
    [1] => 009
    [2] => 072
    [3] => 150.8
    [4] => 
    [5] => 
    [6] => 
    [7] => 06:37:15
)

$line = 'parti 008 069 134.8 P  06:37:58';

// .. 

Array
(
    [0] => parti
    [1] => 008
    [2] => 069
    [3] => 134.8
    [4] => P
    [5] => 
    [6] => 06:37:58
)

然后:

// do something with your data..
$data = [
   // ...
  'TimeofReading' => end($arr),

];

更新:读取为csv文件

使用fgetcsv()

  

fgetcsv()函数从打开的文件中解析一行,检查是否   CSV字段。

     

fgetcsv()函数停止在指定位置的新行中返回   长度或EOF,以先到者为准。

     

此函数在成功或FALSE时返回数组中的CSV字段   关于失败和EOF。

fgetcsv(file,length,separator,enclosure);

答案 1 :(得分:0)

使用正则表达式。由于您的某些行中的字段很少,因此用空格分割可能会导致问题。

preg_match('/(.{5}) (.{3}) (.{3}) (.{5}) (.{1})  (.{8})/', $line, $op);

$data['GunNo'] = $op[1];
$data['FatGun'] = $op[2];
$data['LoinGun'] = $op[3];
$data['ScaleWt'] = $op[4];
$data['Partial'] = $op[5];
$data['TimeofReading'] = $op[6];
echo $op[0];

您可以在

上修改正则表达式行

https://www.phpliveregex.com/#tab-preg-match