try this
$data="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 ~";
$data = array_map('trim',preg_split("/[|~]/",$data));
print_r($data);
Edit: you can have a single line for your entire code.
$data = array_map('trim',preg_split("/[|~]/",file_get_contents('c:\textfile.txt')));
//Check the $parsed below. I think this is enough for adding to your db and to work with.
$parsed = array();
$cntr=-1;
for ($i=0;$i<count($data);$i++){
$kv = explode(':',$data[$i]);
if($kv[0]=='topic')$cntr++;
$k = trim($kv[0]);
if (!empty($k) && !empty($kv[1])){
$parsed[$cntr][$k]=trim($kv[1]);
}
}
print_r($parsed);
The output
Array
(
[0] => Array
(
[topic] => My Thesis
[link] => my_thesis.html
[date] => 04-01-2013, 06
[postedby] => smith
[post] => Has anyone worked on my thesis?
)
[1] => Array
(
[topic] => Thesis Submission
[link] => thesis_submission.html
[date] => 05-11-2013, 10
[postedby] => Smith
[post] => Hurryup We don't have time, Its already late
)
)