I have a .txt file in the following format:
topic: My Thesis | link: my_thesis.html | date: 04-01-2013, 06:01 AM ~ postedby: smith ~ post: Has anyone worked on my thesis? ~
topic: Thesis Submission | link: thesis_submission.html | date: 05-10-2013, 08:20 PM ~ postedby: Terry ~ post: Anyone is working on thesis submission? ~ date: 05-11-2013, 10:04 AM ~ postedby: Julia ~ post: Working on it, will submit today at any time ~ date: 05-11-2013, 04:25 PM ~ postedby: Terry ~ post: Please get reviewed before submission, Thanks! ~ date: 05-11-2013, 10:00 PM ~ postedby: Smith ~ post: Hurryup We don't have time, Its already late ~
I have tried to get its output in this way but could not achieve it. Can anyone help me, please?
topic: My Thesis
link: my_thesis.html
date:04-01-2013, 06:01 AM
postedby:smith
post:Has anyone worked on my thesis?
topic:...
link:..
date:...
postedby:...
post:...
date:...
postedby:...
post:...
.....
.....
Here is my code
$text = file_get_contents('c:\textfile.txt');
$texts = explode("\n",$text);
foreach($texts as $line) {
$temp = explode('topic:',$line);
$topic = explode("|",$temp[1]);
echo "topic : " .$topic[0] ."<br/>";
$temp = explode('link:',$line);
$href = explode("|",$temp[1]);
echo "link : " .$topic[0] ."<br/>";
$add = explode("|",$line);
$adds = $add[2];
$rem = explode("\n",$adds);
foreach($rem as $data) {
$temps = explode('date:',$data);
$date = explode('~',$temps[1]);
echo "date : " .$date[0] ."<br/>";
$temps = explode('postedby:',$data);
$postedby = explode('~',$temps[1]);
echo "postedby: " .$postedby[0] ."<br/>";
$temps = explode('post:',$data);
$post = explode('~',$temps[1]);
echo "post: " . $post[0] ."<br/>";
}
}
SimpleXMLand save it under a proper format. You will always run into errors with such files, for example when your data contains seperators liketopic: My Thesis | link: my_thesis.html?category=test|one|two | date: ...$texts = file('C:\textfile.txt');