我能';t按我需要的格式分解文本文件。。php
我在按所需格式分解文本文件时遇到问题: 我有一个以下格式的文本文件: 作者=cermin_mata发布日期=20 100 02 15 01:45:00.0 随便 作者=奥列格·布图佐夫发布日期=2010021503:34:00。0 随便。。。。。。。。 我想将每个帖子及其信息存储在数据库中。例如,我想将文本文件拆分为帖子,并以相同的格式存储每篇帖子(第一行是作者信息,第二行是帖子): 作者=cermin_mata发布日期=20 100 02 15 01:45:00.0 无论什么 为此,我使用了以下代码:我能';t按我需要的格式分解文本文件。。php,php,sql,regex,arrays,explode,Php,Sql,Regex,Arrays,Explode,我在按所需格式分解文本文件时遇到问题: 我有一个以下格式的文本文件: 作者=cermin_mata发布日期=20 100 02 15 01:45:00.0 随便 作者=奥列格·布图佐夫发布日期=2010021503:34:00。0 随便。。。。。。。。 我想将每个帖子及其信息存储在数据库中。例如,我想将文本文件拆分为帖子,并以相同的格式存储每篇帖子(第一行是作者信息,第二行是帖子): 作者=cermin_mata发布日期=20 100 02 15 01:45:00.0 无论什么 为此,我使用
<?php
$author='AUTHOR';
$word_escaped = preg_quote($author, '~');
$pattern='~\b' . $word_escaped . '\b~';
$count=preg_match_all($pattern, $fcount,$matches);
$fp=fopen("ENG333","r");
while(!feof($fp))
{
$contenet1=fgets($fp);
$contenet=trim($contenet1);
if (strcmp($contenet,"")){
$contenet1=str_replace("،"," ، ",$contenet);
$contenet2=str_replace(","," , ",$contenet1);
$contenet1=str_replace("?"," ? ",$contenet2);
$contenet2=str_replace("."," .",$contenet1);
$contenet=str_replace(" "," ",$contenet2);
//echo "'$author' occures $count time(s).";$author='AUTHOR';
//echo $count;
$posts=explode($pattern,$contenet);
print_r($posts);
}
}
?>
我得到的结果是:
Array ( [0] => AUTHOR = Fern pot date=20 1 0 02 15 01 : 45 : 00 .0 )
Array ( [0] => whatever........)
Array ( [0] => AUTHOR = Oleg Butuzov post date = 20 1 0 02 15 03 : 34 : 00 . 0 )
Array ( [0] => whatever........)
数组([0]=>AUTHOR=Fern pot date=20 1 0 02 15 01:45:00.0)
数组([0]=>whatever……)
数组([0]=>AUTHOR=Oleg Butuzov发布日期=201 0 02 15 03:34:00.0)
数组([0]=>whatever……)
虽然我需要输出如下所示:
Array ( [0] => AUTHOR = Fern pot date = 20 1 0 02 15 01 : 45 : 00 . 0
whatever........)
Array ( [1] => AUTHOR = Oleg Butuzov post date = 20 1 0 02 15 03 : 34 : 00 . 0
whatever........)
数组([0]=>AUTHOR=Fern pot date=201 0 02 15 01:45:00.0)
无论如何……)
数组([1]=>AUTHOR=Oleg Butuzov发布日期=201 0 02 15 03:34:00.0
无论如何……)
我尝试使用file_get_contents(),但仍然存在相同的问题
谢谢你的建议 你试过这个正则表达式吗?对我来说似乎还可以:
/(AUTHOR.*?)(\n|\r)+(?<!AUTHOR)(.+)/
/(AUTHOR.*)(\n |\r)+(?我建议您使用xml
Array ( [0] => AUTHOR = Fern pot date = 20 1 0 02 15 01 : 45 : 00 . 0
whatever........)
Array ( [1] => AUTHOR = Oleg Butuzov post date = 20 1 0 02 15 03 : 34 : 00 . 0
whatever........)
/(AUTHOR.*?)(\n|\r)+(?<!AUTHOR)(.+)/