문제

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/>";  
    }
}
도움이 되었습니까?

해결책

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
        )

)

다른 팁

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.

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";
  }
}

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);
?>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top