Php reads txt data by line and stores it in mysql

Source: Internet
Author: User
Php reads txt data by line and stores it in mysql
58131 2012   041181603358---------------  278    4---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------V1.000021 6773 6760 7201 7266 7162 7393 7427 7574 7440 7303 7321 7440 6927 7233 7219 7504 7555 7458 7518 7833 7852 7652 7040 6672 6699 5926 5717 5738 6073 6056 6325 6352 6965 7062 7094 7278 7465 7292 7324 7475 7562 7546 7314 7821 7869 7814 8178 8020 8300 7930 8608 8833 8643 8910 8927 8857 8976 8798 9535 90090022 9028 9818 9304 9042 8616 9044 9664 9966 970810022 962010491 9878 955810206100981000010564 99021020910279 8850 9246 9647 9310 8204 7860 8477 8313 8029 6600 6945 6503 6377 6212 5610 5732 5321 5302 5530 5973 5057 4293 4014 4826 5012 4955 4643 5218 5708 5861 6365 6756 6637 6534 8180 6732 6103 7344 7051


The above is the txt format file data content, the file name is "V5813101.012"
Format: The first line. The description of the data file is as follows:
Serial number parameter long standby note
1. file number 5 bytes
2. less than 5 bytes per year, leading space
3. 5 bytes per month
4. the length is 5 bytes.
5. 5 bytes in width
6. use "-" (minus sign) to pre-fill each byte with 15 bytes of the standby field.
7. leading space when the height is 5 bytes high or not.
8. Mark 5 bytes to "4"
9. fill the subsequent reserved bytes with "-" (minus signs) to make the parameter line the same length as the subsequent data record line.
10. V1.00 4 bytes
11. press enter to wrap 2 bytes

Starting from the second line
Starting from the first row of the file, the minute data record line is a record line every hour, the length of each record is equal, the record length is 60 × 5 + 6. The order is: 2 "dates", 2 "hours", 60 Minutes * 5 + 2 "carriage return ".

One month is a file, and there is a simple way to read data and write it into the database.
Full score.


Reply to discussion (solution)

$ Fn = 'v5813101. 012 '; $ ar = file ($ fn, FILE_IGNORE_NEW_LINES | FILE_IGNORE_NEW_LINES); // process the first line $ h = sscanf (str_replace ('','_', $ ar [0]), "% 5s % 5s % 5s % 5s % 5s % 5s % 5s % 15 s % 5s % 5s % 5s % 249 s % 5s"); array_walk ($ h, function (& $ v, $ k, $ t) {$ v = strtr ($ v, $ t) ;}, array ('_' => '')); print_r ($ h); // process the remaining rows for ($ I = 1; $ I
  
  

You can only say that you are too good !!
But there is another problem.
Error reported in row 7th
Parse error: syntax error, unexpected T_FUNCTION in read. php on line 7
If this line is commented out, it is printed out.

$ Fn = 'v5813101. 012 '; $ ar = file ($ fn, FILE_IGNORE_NEW_LINES | FILE_IGNORE_NEW_LINES); // process the first line $ h = sscanf (str_replace ('','_', $ ar [0]), "% 5s % 5s % 5s % 5s % 5s % 5s % 5s % 15 s % 5s % 5s % 5s % 249 s % 5s"); array_walk ($ h, function (& $ v, $ k, $ t) {$ v = strtr ($ v, $ t) ;}, array ('_' => '')); print_r ($ h); // process the remaining rows for ($ I = 1; $ I
    
    

Is 6th rows

You can only say that you are too good !!
But there is another problem.
Error reported in row 7th
Parse error: syntax error, unexpected T_FUNCTION in read. php on line 7
If this line is commented out, it is printed out.


$ Fn = 'v5813101. 012 '; $ ar = file ($ fn, FILE_IGNORE_NEW_LINES | FILE_IGNORE_NEW_LINES); // process the first line $ h = sscanf (str_replace ('','_', $ ar [0]), "% 5s % 5s % 5s % 5s % 5s % 5s % 5s % 15 s % 5s % 5s % 5s % 249 s % 5s"); array_walk ($ h, function (& $ v, $ k, $ t) {$ v = strtr ($ v, $ t) ;}, array ('_' => '')); print_r ($ h); // process the remaining rows for ($ I = 1; $ I
      
      

You haven't used php5.3 yet?
You can write a callback function separately.

array_walk($h, 'foo', array('_' => ' '));function foo(&$v, $k, $t) { $v = strtr($v, $t); }


The purpose of this callback is to change the space previously changed "_".

Yes, it should be a version Issue. thank you!

You haven't used php5.3 yet?
You can write a callback function separately.

array_walk($h, 'foo', array('_' => ' '));function foo(&$v, $k, $t) { $v = strtr($v, $t); }


The purpose of this callback is to change the space previously changed "_".

You haven't used php5.3 yet?
You can write a callback function separately.
array_walk($h, 'foo', array('_' => ' '));function foo(&$v, $k, $t) { $v = strtr($v, $t); }


The purpose of this callback is to change the space previously changed "_".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.