Page 1 of 1

Need Help Parsing text

Posted: Sun Sep 05, 2010 8:50 am
by lee1978
Hi, Guys&Girls

I am new to the site and need help parsing a file the file has a max of 2000 line and looks like this

1006:93:89:16,6473:94:21:33
1009:47:13:95,624:15:78:4
1019:53:77:46,4547:29:42:78
1022:39:26:64,39:52:59:99
1024:48:28:52,7087:29:70:75

i need to convert the above so i can insert it directly in to an sql database it need to look like this exactly not sure what is the best way to do it either somekind of script to convert the file or a script sql script new to all this but getting there at the moment i have to do it manually which just takes me hours thanks in advance for your help

INSERT INTO `de_Coordonnee` (`ID`, `TYPE`, `POSIN`, `POSOUT`, `COORDET`, `COORDETOUT`, `NOTE`, `UTILISATEUR`, `udate`) VALUES
( , 1, '1006', '6473', '93-89-16', '94-21-33', '', 'Lee', 1283682460),

Re: Need Help Parsing text

Posted: Sun Sep 05, 2010 1:24 pm
by Jonah Bron

Code: Select all

$file = file('my_file.txt', FILE_IGNORE_NEW_LINES|FILE_SKIP_EMPTY_LINES);
foreach ($file as $line) {
    $line = explode(':', $line);
    $line[3] = explode(',', $line[3]);
    $query = 'INSERT INTO `de_Coordonnee`
        (`TYPE`, `POSIN`, `POSOUT`, `COORDET`, `COORDETOUT`, `NOTE`, `UTILISATEUR`, `udate`)
        VALUES
        (
            1,
            "' . $line[3][1] .'",
            "' . $line[1] . '-' . $line[2] . '-' . $line[3][0] . '",
            "' . $line[4] . '-' . $line[5] . '-' . $line[6] . '",
            "Lee",
            1283682460
        )';
    // run query
}
That should parse it properly. I'm guessing "Lee" and "1283682460" are constant?