Failing_Solutions Posted June 2, 2012 Share Posted June 2, 2012 Hello, I'm working with a text file, what I ultimately want to do is parse the file and write contents to the database. Much of this I understand but I'm still new at working with files so I have a question regarding how to best handle it. The file; I have no control over how the file is given to me so I have to deal with it as is. Example: 1036562, Infiltrator il-Kor, 1, Very Long, Crucias, 5 1036639, Thieving Magpie, 1, Very Long, Varulf, 4 1036644, Gaseous Form, 1, Very Long, Varulf, 5 The first step is where I'm stuck, before writing anything to the database I'm trying to view the array, which is showing me a problem. Here is the code I'm using to break up the file: <?php $file = 'trades.dat'; $str =file_get_contents($file,'a') or die("can't open file"); //Replace Card with Comma in its name $find='/Eladamri,/'; $replace ='Eladamri'; $str=preg_replace($find,$replace,$str); if (is_string($str) && $str !="") { $print=explode(",",trim($str)); } print_r($print); ?> The problem is exploding the string on the comma works, but joins the last value and the first value of the next line in the text file together like this array([0]=>1036562[1]=>Infiltrator il-Kor[2]=>1[3]=>Very Long[4]=>Crucias[5]=> 5 1036639) should be: array([0]=>1036562[1]=>Infiltrator il-Kor[2]=>1[3]=>Very Long[4]=>Crucias[5]=> 5[6]=>1036639.....) I have tried several things that I can think but nothing seems to work, any advice would be helpful Thank you Quote Link to comment Share on other sites More sharing options...
Barand Posted June 2, 2012 Share Posted June 2, 2012 check out fgetcsv Quote Link to comment Share on other sites More sharing options...
Failing_Solutions Posted June 3, 2012 Author Share Posted June 3, 2012 Thanks for the reply I looked at it, and can't get it do do anything. The problem I find with that function is that I have one card that has a comma in the name and I have to fix that first. So far I've come up with no solutions. Quote Link to comment Share on other sites More sharing options...
Failing_Solutions Posted June 3, 2012 Author Share Posted June 3, 2012 fixed my problem by adding another comma at the last line (seen here..http://www.phpfreaks.com/forums/index.php?topic=360366.0) Thanks for the suggestion Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.