Justin Koivisto
10/7/2003 7:32:00 PM
Artco News wrote:
> I thought I ask the scripting guru about the following.
>
> I have a file containing records of data with the following format(first
> column is the label):
>
> CODE#1^DESCRIPTION^CODE#2^NOTES
> NN-110^an info of NN-001^BRY234^some notes
> NN-111^1st line data
> 2nd line data
> 3rd line data^BRT345^another notes
> NN-112^description of NN-112^BBC23^multiline
> notes blah
> blah
> blah
> NN-113^info info^MNO12^some notes here
>
> How do I parse so I can insert them in the database, e.g. MySQL/Access?
>
> Perhaps there are an advanced scripting language can do this easily.
Regex is your friend...
<?php
$fp=fopen('data.txt','r');
$content=fread($fp,filesize('data.txt'));
fclose($fp);
$tmp=time();
$content= preg_replace('/(\r\n|\r|\n)/',$tmp,$content);
$pattern='/NN-111\^(.*)\^/U';
preg_match($pattern,$content,$matches);
$data=explode($tmp,$matches[1]);
unset($matches);
unset($content);
unset($time);
echo '<pre>';
print_r($data);
echo'</pre>';
?>
This will get you an array with each line of data as a separate element.
You should be able to see how to extract the notes and such from the
example. I may be wrong, but it looks like the caret (^) is used as a
field delimiter as well as the newline.
--
Justin Koivisto - spam@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.