Can PHP script read a text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crimmelcp
Forum Newbie
Posts: 16
Joined: Mon Jan 28, 2008 8:48 pm

Can PHP script read a text file

Post by crimmelcp »

Can PHP script read a text file

Can someone show me?

I need to import a text file into a sql database

Thanks
Charlie Crimmel
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can PHP script read a text file

Post by s.dot »

file_get_contents()
fopen(), fread(), fclose()
fgetcsv()

and for mysql LOAD DATA IN FILE
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
crimmelcp
Forum Newbie
Posts: 16
Joined: Mon Jan 28, 2008 8:48 pm

Re: Can PHP script read a text file

Post by crimmelcp »

Thanks:
This is my first attempt to use MySQL and PHP
If I had a Vendor text file with the following fields
Vendor_ID , Vendor_Number , Vendor_name, Address1, Address2, City, State, Zip
the data might look like
0001,"V12345","Joe Smith","123 Any place Street","Apt. 123","Any City","WV","11111"
0002,"V23456","John Smith","234 Any place Street","Apt. 123","Any City","OH","2222"
0003,"V34567","Bob Smith","345 Any place Street","Apt. 123","Any City","KY","33333"
0004,"V45678","Mary Smith","456 Any place Street","Apt. 123","Any City","IN","44444"
0005,"V56778","Sue Smith","567 Any place Street","Apt. 123","Any City","TN","55555"

What would be the syntax to read this text file and import it into the Vendor SQL Table with the same fields.

Thanks
Charlie Crimmel
dayyanb
Forum Commoner
Posts: 46
Joined: Wed Jan 23, 2008 12:34 am

Re: Can PHP script read a text file

Post by dayyanb »

crimmelcp wrote:Thanks:
This is my first attempt to use MySQL and PHP
If I had a Vendor text file with the following fields
Vendor_ID , Vendor_Number , Vendor_name, Address1, Address2, City, State, Zip
the data might look like
0001,"V12345","Joe Smith","123 Any place Street","Apt. 123","Any City","WV","11111"
0002,"V23456","John Smith","234 Any place Street","Apt. 123","Any City","OH","2222"
0003,"V34567","Bob Smith","345 Any place Street","Apt. 123","Any City","KY","33333"
0004,"V45678","Mary Smith","456 Any place Street","Apt. 123","Any City","IN","44444"
0005,"V56778","Sue Smith","567 Any place Street","Apt. 123","Any City","TN","55555"

What would be the syntax to read this text file and import it into the Vendor SQL Table with the same fields.

Thanks
Charlie Crimmel
Use preg_match_all to extract the individual values, then insert them into your database.
crimmelcp
Forum Newbie
Posts: 16
Joined: Mon Jan 28, 2008 8:48 pm

Re: Can PHP script read a text file

Post by crimmelcp »

Thanks:
If I were doing this in another programming language I would DIM variables
DIM vVendor_ID as N
Dim vVendor_Number as C
Dim vVendor_name as C
Dim vAddress1 as C
Dim vAddress2 as C
Dim vCity as C
Dim vState as C
Dim vZip as C
Then initializee the variables
vVendor_ID = 0
vVendor_Number =""
vVendor_Name=""
vAddress1=""
vAddress2=""
vVendor_City=""
vVendor_State=""
vVendor_Zip=""
Then open the file
read the first record
populate the variables
write the record to the Table
read the next record
loop until the EOF

Is this the same concept as preg_match_all
or does PHP have a way to do it like described above.

Thanks
Charlie Crimmel
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Can PHP script read a text file

Post by s.dot »

You would be much better off using fgetcsv() as it can detect individual values using your comma delimeters.

There's some good examples on the linked page.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dayyanb
Forum Commoner
Posts: 46
Joined: Wed Jan 23, 2008 12:34 am

Re: Can PHP script read a text file

Post by dayyanb »

scottayy wrote:You would be much better off using fgetcsv() as it can detect individual values using your comma delimeters.

There's some good examples on the linked page.
But I wrote a regex : (. /(?:"?(.*?)"?)(?:,|\n|\r|$)+/
Post Reply