Comma delimited multiple records txt file
Posted: Mon Oct 08, 2007 12:58 pm
Hi guys,
So long I've not been here, hopefully someone here still remember who I am.
Alright, let's back to the topic. Previously I was creating a text to XML converter, which users can upload text files and convert them into XML files.
At first, each text file consists of a record. Now, I need to modify it for each text file consists of multiple records. Therefore, I will need to separate those records by using comma or any other suitable characters.
The First Method:
Previously, the only one record in the text file would look like this:
The Second Method:
Now, I want it to have more records in a text file, and convert all the records into separate XML files. My idea is to organize them like this:
My first attempt is for the first method is shown below:
For the second method, I've some ideas now, I scan the first record at first row/line, then create the XML file for it. Then I move on to the next line and do the same things.
Or,
I scan through all the contents in the text file, find all the predefined delimiters and separate them into multiple records. Then only I loop through the records and convert them into XML.
I don't know which flow is the correct one, and don't know whether what I'm thinking is correct. Please help me...
Thanks in advance!
So long I've not been here, hopefully someone here still remember who I am.
Alright, let's back to the topic. Previously I was creating a text to XML converter, which users can upload text files and convert them into XML files.
At first, each text file consists of a record. Now, I need to modify it for each text file consists of multiple records. Therefore, I will need to separate those records by using comma or any other suitable characters.
The First Method:
Previously, the only one record in the text file would look like this:
Code: Select all
name: phpwalker
age: 16
phone: 12345678Now, I want it to have more records in a text file, and convert all the records into separate XML files. My idea is to organize them like this:
Code: Select all
phpwalker, 16, 12345678;
Alex, 17, 321654987;
phpfreak, 13, 22334460;
Mary, 15, 321654978;Code: Select all
$file = $uploadfile;
$data = file($file) or die('Could not read file!');
foreach ($data as $line_num => $line) {
if (preg_match("/^\w+\W\s\w+/", $line)) {
list($field,$contents) = explode(":", $line);
$contents =htmlentities($contents);
$str .="<$field>$contents</$field>\n";
}Or,
I scan through all the contents in the text file, find all the predefined delimiters and separate them into multiple records. Then only I loop through the records and convert them into XML.
I don't know which flow is the correct one, and don't know whether what I'm thinking is correct. Please help me...
Thanks in advance!