繁体   English   中英

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

[英]How to read columns from text file and insert in database php

我有一个文本文件,里面有多个行和列。

我想读取每一行和每一行,将它们存储在数组中并使用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

尝试这样的事情:

// 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(以先到者为准)返回。

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

fgetcsv(file,length,separator,enclosure);

使用正则表达式。 由于您有一些行空间很少,因此空间拆分可能会导致问题。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM