1

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/>";  
    }
}
6
  • 1
    What isn't working correctly? Commented Jul 30, 2013 at 10:11
  • 1
    can't you just use str_replace and replace the | with a newline? Commented Jul 30, 2013 at 10:11
  • why you don't use SimpleXML and save it under a proper format. You will always run into errors with such files, for example when your data contains seperators like topic: My Thesis | link: my_thesis.html?category=test|one|two | date: ... Commented Jul 30, 2013 at 10:11
  • @Neils, The issue is with second for loop, Its not working correctly in case of multiple posts Commented Jul 30, 2013 at 10:20
  • sidenote: your first 2 lines can be substituted with $texts = file('C:\textfile.txt'); Commented Jul 30, 2013 at 15:29

4 Answers 4

1

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
        )

)
Sign up to request clarification or add additional context in comments.

1 Comment

Solution is good but i have to insert these values in db, topic and link in one table and rest in other table
1

You can easily do this with strtok

strtok() splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token. That is, if you have a string like "This is an example string" you could tokenize this string into its individual words by using the space character as the token.

Example (demo):

$txt = 'topic: My Thesis | link: …';

for ($t = strtok($txt, "|~"); $t !== false; $t = strtok("|~")) {
    echo trim($t), PHP_EOL;
}

This will give the output you show in your question.

Comments

0

Here's a working implementation of what I think you want:

$lines = explode("\n", $input);
foreach($lines as $line)
{
  echo "----\n";
  $elements = preg_split('#[~|]#', $line);
  foreach($elements as $element)
  {
    list($key, $value) = array_map('trim', explode(':', $element));
    echo "$key -> $value\n";
  }
}

Comments

0

Try this

<?php
$lines = file("textfile.txt");
//print_r($lines);

$data = array();
for($i = 0; $i < count($lines); $i += 2)
{
    $data[] = array_map('trim',preg_split("/[\|~]/",$lines[$i]));

}
print_r($data);
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.