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!