我遇到了以我需要的格式爆炸文本文件的问题:
我有一个这种格式的文本文件:
AUTHOR = cermin_mata post date=20 1 0 02 15 01 : 45 : 00.0whatever........
AUTHOR = Oleg Butuzov post date = 20 1 0 02 15 03 : 34 : 00 . 0
whatever........
我想将每个帖子的信息存储在数据库中。例如,我想将文本文件拆分为帖子,并以相同的格式存储每个帖子(第一行中的作者信息和第二行中的帖子):
AUTHOR = cermin_mata post date=20 1 0 02 15 01 : 45 : 00.0 whatever........
为此,我使用了以下代码:
<?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........)
虽然我需要输出如下:
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........)
我尝试使用file_get_contents()但我仍然遇到同样的问题。
感谢您的任何想法。
答案 0 :(得分:0)
你试过这个正则表达式吗?它对我来说似乎没问题:
/(AUTHOR.*?)(\n|\r)+(?<!AUTHOR)(.+)/