homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

how to convert txt file into csv using php

 9:35 am on Apr 16, 2010 (gmt 0)

Hi guys,

i need a solution/ code to convert txt file values into a csv file and the csv file should be inserted into Phpmyadmin

Actually the text file contains the results of a student...etc

Please help me out as ASAP




 11:13 am on Apr 16, 2010 (gmt 0)

it depends how the .txt file is arranged at the moment. you should paste up an example here.
you need to make it so each 'field' in the database has an entry in the text file. which means you might have to introduce some blanks, if they are missing.
if all the data in the text file is already seperated out with commas, or semi-colons, or whatever, then it could be as simple as just renaming the file type extension to .csv


 4:11 pm on Apr 16, 2010 (gmt 0)

what does the text file content look like?


 6:16 am on Apr 17, 2010 (gmt 0)


my text file looks like this

050102044101IPPILI DEVI ------------------------------------------ A -999999
050102049201JAMI MADHAVI 0135 *P0342 *P5105 F5535 *P6113 F-------130FAIL -999999
050102053301BEHARA ANURADHA 0138 *P0350 *P5113 F5530 F6137 *P-------168FAIL -999999
050102058401TOLAPU ADILAKSHMI 0110 F0335 *P5100 F5704 F6104 F-------053FAIL -999999
050102063501HARSHA VALLI BHAIRETHI 0156 *P0381 *P3127 P3214 F4113 F4221 *P212FAIL -999999
050102085601RAJESWARI PEDADA 0136 *P0349 *P5108 F5741 *P6106 F-------140FAIL -999999
050102087701NAKKA PADMAVATHI 0136 *P0348 *P5105 F5735 *P6113 F-------137FAIL -999999
050102093801MANDANGI AMARAVATHI 0145 *P0371 *P5111 F5735 *P6151 *P-------213FAIL -999999
050102097901DONDAPATI RADHA 0135 *P0374 *P3101 F3209 F4109 F4221 *P149FAIL -999999
050102110101KANUGULA RESHMA 0135 *P0364 *P5107 F5519 F6122 F-------147FAIL -999999
050102112201MALAPETA JHANSI RANI 0135 P0373 *P3708 F3821 *P4109 F4221 *P167FAIL -999999
050102114301BALAGA SYAMALASANJEEVI ------------------------------------------ A -999999
050102118401KARAJADA SATYAVATHI 0139 *P0352 *P5108 F5509 F6140 P-------148FAIL -999999

Its all scrambled.....hallticket no and name are merged in one line....failed students marks can be visible...the student who passes his marks are not visible he can only view his grade......

totally its very scrambled

w8ting for u r solutions



 10:53 am on Apr 17, 2010 (gmt 0)

if the hash lines are uniform across the whole file, then you can use preg_replace to replace every seven hashes with two commas, which will turn them into blank fields.

and i would replace every space with a comma as well, which will take care of most of the rest, but then you will have a problem with their names, because it will split up their christian/middle/surnames.
you'd have to come up with a regular expression which only replaces spaces with commas if one of the characters on either side is a non A-Z character.
im sure one of the brainboxes here will know how to do it.


 3:04 pm on Apr 19, 2010 (gmt 0)

it doesn't seem scrambled

12 chars == id
from 13 -> first nonalpha preceded by a space == first/last name (could also just be space first space last space)
next 43 chars == something, not sure what but its 43 chars (the trailing space is included in the field as it disappears sometimes)
then the mark, either FAIL or a grade, you would have to look and see if any - or + occur
then finished by a space and -999999 not sure if that changes but you could look at the file

whoever produces this file could probably give you a format


 5:59 am on Apr 20, 2010 (gmt 0)

thanks for the replys...i will test and get back to you


 6:46 am on Apr 20, 2010 (gmt 0)

guys u r ideas have been worked........many many thanks nice solutions.........


 8:16 am on Apr 24, 2010 (gmt 0)

I hope those aren't real names and real personal data.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved