简体   繁体   English

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

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

I have a text file with multiple rows and columns inside them. 我有一个文本文件,里面有多个行和列。

I want to read each and every row and column, store them in array and save all data in database using cakephp. 我想读取每一行和每一行,将它们存储在数组中并使用cakephp将所有数据保存在数据库中。

Below is my code, I wrote some row and column reading logic which i wants to implement. 下面是我的代码,我写了一些我想要实现的行和列读取逻辑。

Please help me to do this critical thing. 请帮我做一件至关重要的事情。

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

My file data 我的档案资料

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

Try something like this: 尝试这样的事情:

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

Update with some test: 更新一些测试:

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

Then: 然后:

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

];

Update: reading as csv file 更新:读取为csv文件

Use fgetcsv() 使用fgetcsv()

The fgetcsv() function parses a line from an open file, checking for CSV fields. fgetcsv()函数从打开的文件中解析一行,检查CSV字段。

The fgetcsv() function stops returning on a new line, at the specified length, or at EOF, whichever comes first. fgetcsv()函数停止在新行,指定长度或EOF(以先到者为准)返回。

This function returns the CSV fields in an array on success, or FALSE on failure and EOF. 此函数在成功时返回数组中的CSV字段,或在失败和EOF时返回FALSE。

fgetcsv(file,length,separator,enclosure);

Use regular expression. 使用正则表达式。 As you have some rows having few fields empty splitting with space might cause problem. 由于您有一些行空间很少,因此空间拆分可能会导致问题。

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];

You can teds regular expression line on 你可以在正则表达式线上打开

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

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

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