Jump to content

Help with working with text files


Recommended Posts

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.