1

我有以下格式的 .txt 文件:

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 ~ 

我试图以这种方式获得它的输出,但无法实现。任何人都可以帮助我吗?

主题:我的论文
链接:my_thesis.html
日期:04-01-2013,06:01 AM
发布者:smith
帖子:有人研究过我的论文吗?
主题:...
链接:..
日期:... 发帖
人:...
发帖人:... 发帖人:... 发帖 人:... 发帖人:... ..... .....





这是我的代码

$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/>";  
    }
}
4

4 回答 4

1

尝试这个

$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);

编辑:您的整个代码可以有一行。

$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);

输出

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
        )

)
于 2013-07-30T10:20:02.137 回答
1

你可以很容易地做到这一点strtok

strtok()将字符串 ( str) 拆分为更小的字符串 ( tokens),每个标记由标记中的任何字符分隔。也就是说,如果您有一个类似“This is an example string”的字符串,您可以通过使用空格字符作为标记将此字符串标记为单独的单词。

示例(演示):

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

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

这将给出您在问题中显示的输出。

于 2013-07-30T10:37:23.407 回答
0

这是我认为您想要的工作实现:

$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";
  }
}
于 2013-07-30T10:18:02.110 回答
0

尝试这个

<?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);
?>
于 2013-07-30T10:22:03.937 回答