0

I have read some posts where a php file opens and writes the info to a database ,but it has a delimiter such as | or , or .. or ' '

However, I have a txt file that deals with numbers and the delimiter is not present in numbers. But i want to store that huge numbers depend upon there description.

That Description of number store in different column of the mysql database line by line

For example :

34012015000220001000631931058620000607262Y22122014 N                                                                                                                                                                                                                                                                                                                    
34012015000220002000637717660206000607262Y23122014 N                                                                                                                                                                                                                                                                                                                    
34012015000220003000647245251009000607262Y23122014 N                                                                                                                                                                                                                                                                                                                    

Here is 34 is constant value.

        012015000220001-record No.
        000631931058620-Aadhar No.
        000607262-BIN no.
        Y-record status
        22122014-date
         -one blank space
        N-Mapping Status

Each new line is a new customer with their individual details.

So please sir give me any solution to solve this problem?

Farvardin
  • 5,336
  • 5
  • 33
  • 54

3 Answers3

1

you can do something like this, If you have same length of numbers.

$string = "34012015000220001000631931058620000607262Y22122014 N";
$constant = substr($string, 0, 2);
$record_no = substr($string, 2, 18);
$Aadhar = substr($string, 17, 14); // and so on...
print_r($Aadhar); 
mohit
  • 1,878
  • 1
  • 16
  • 27
  • But the File has huge contents which is large no of line in this text file .So i want to store each line depend upon there decription ( i.e. constant,record no,aadhar no etc.) store in the database which insert the whole text file data line by line. – sandesh sawant Apr 25 '15 at 09:28
  • But the File has huge contents which is large no of line in this text file .So i want to store each line depend upon there decription ( i.e. constant,record no,aadhar no etc.) store in the database which insert the whole text file data line by line.It means every line in text file which store in database in each line. – sandesh sawant Apr 25 '15 at 09:39
0

You may split each line by unpack or sscanf function

example1

$line = '34012015000220001000631931058620000607262Y22122014 N';
$fields = sscanf($line, '%2s%15s%15s%9s%1s%8s %1s');
print_r($fields); 

it prints:

Array
(
    [0] => 34
    [1] => 012015000220001
    [2] => 000631931058620
    [3] => 000607262
    [4] => Y
    [5] => 22122014
    [6] => N
)

example2

$line = '34012015000220001000631931058620000607262Y22122014 N';
$fields = unpack('A2const/A15recordNo/A15aadharNo/A9binNo/A1date/A8date/A1/A1mappingStatus', $line);
print_r($fields); 

it prints:

Array
(
    [const] => 34
    [recordNo] => 012015000220001
    [aadharNo] => 000631931058620
    [binNo] => 000607262
    [date] => 22122014
    [1] => 
    [mappingStatus] => N
)
tutankhamun
  • 880
  • 2
  • 11
  • 21
0

First of all, shoot the person who designed the text file.

Secondly, parse it using a simple substr() assuming that each field is of constant length:

// create your sql, prepare it, bind it 
$link = mysqli_connect("localhost", "my_user", "my_password", "world");

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %sn", mysqli_connect_error());
    exit();
}

/* Prepare an insert statement */
$query = "INSERT INTO table (rec, aadhar, ...) VALUES (?,?,...)";
$stmt = mysqli_prepare($link, $query);

mysqli_stmt_bind_param($stmt, "ss...", $recno, $aadhar, ...);

while ($ln = fgets($fh)) {
    $recno = substr($ln, 0, 20);
    $aadhar= substr($ln, 21, 12);
    ...
    /* Execute the statement */
    mysqli_stmt_execute($stmt);
}

/* close statement */
mysqli_stmt_close($stmt);

SOURCE: Example modified from http://php.net/manual/en/mysqli-stmt.execute.php

Peter Bowers
  • 3,063
  • 1
  • 10
  • 18