Reputation: 123
I have to do a task of importing data to mysql from a text file using php code, yeah it's sound so easy, and I have already done that before like importing data from csv file, from excel file or any text file where data is separated with any delimiter. But in my current case, there is no any delimiter just two spaces and the fix length of field. For example-
table field
|------|-----------|-----------|-------------|
| id(8)| name(50) | state(15) | category(10)|
|------|-----------|-----------|-------------|
| | | | |
sample data of upload.txt file-
::format::
ID NAME ADDRESS CATEGORY
10719922 Union Bank of India delhi normal
10719956 State Bank of India mumbai normal
10719522 HDFC Bank gujrat high
10759924 ICICI Bank goa normal
Now you can understand the data format of text file, i.e. field length + two spaces, field length + two spaces and so on. The problem is if the data is not matches with the field size then again spaces are hare to complete the length of field that's why two space are not available as delimiter. Like take the first of data- id have 8 digit data than two spaces and name length 50 but data have only 19 character so there is 31 spaces to complete the length 50 after that two space then next field. So I have no delimiter or syntax (rather than length + 2 spaces) to identify the single field data. I am very confused how to import this data to MySQL using php script. does any one think It can be happen. Please I need some idea or php code, to handle this situation. Thank You
Upvotes: 1
Views: 2214
Reputation: 97331
It shouldn't be more difficult than this:
<?php
$input = <<<END
10719922 Union Bank of India delhi normal
10719956 State Bank of India mumbai normal
10719522 HDFC Bank gujrat high
10759924 ICICI Bank goa normal
END;
$def = array(
"id" => 8,
"name" => 50,
"state" => 15,
"category" => 10
);
foreach (explode(PHP_EOL, $input) as $line) {
foreach ($def as $field => $length) {
$value = substr($line, 0, $length + 2);
$line = substr($line, $length + 2);
print $field.' = '.trim($value).PHP_EOL;
}
print '----------------------------------------'.PHP_EOL;
}
?>
Basic idea is to create a format definition in the $def
hash, and then process all lines according to that format definition.
Executing this code will yield the output below. Change the actual implementation to fit your needs.
id = 10719922
name = Union Bank of India
state = delhi
category = normal
----------------------------------------
id = 10719956
name = State Bank of India
state = mumbai
category = normal
----------------------------------------
id = 10719522
name = HDFC Bank
state = gujrat
category = high
----------------------------------------
id = 10759924
name = ICICI Bank
state = goa
category = normal
----------------------------------------
Upvotes: 1
Reputation: 43582
You could use preg_split()
function, and explode the string/line/row by >= 2 spaces
:
$line = '10719922 Union Bank of India delhi normal';
$m = preg_split('~(\h{2,})~', $line);
print_r($m);
Upvotes: 0