我有一个.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
发布者:史密斯
帖子:有没有人参与我的论文?
话题:...
链接:..
日期:...
postedby:...
后:...
日期:...
postedby:...
后:...
.....
.....
这是我的代码
$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/>";
}
}
答案 0 :(得分: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
)
)
答案 1 :(得分:1)
您可以使用strtok
strtok()
将字符串(str
)拆分为较小的字符串(tokens
),每个标记由令牌中的任何字符分隔。也就是说,如果您有一个类似“这是一个示例字符串”的字符串,您可以使用空格字符作为标记将此字符串标记为其单个单词。
$txt = 'topic: My Thesis | link: …';
for ($t = strtok($txt, "|~"); $t !== false; $t = strtok("|~")) {
echo trim($t), PHP_EOL;
}
这将显示您在问题中显示的输出。
答案 2 :(得分: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";
}
}
答案 3 :(得分: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);
?>