如何使用原始文本数据创建csv文件

时间:2014-06-06 08:37:28

标签: php excel csv excel-formula export-to-csv

关于csv文件创建和在数据库中插入数据,我很困惑。 假设我有以下文本数据 - 这是45000记录集,我在下面发布它们的露水。

Winged Wheels in France, by Michael Myers Shoemaker                      45790

A Battle Fought on Snow Shoes, by Mary Cochrane Rogers                   45789

The German Classics of the Nineteenth and Twentieth Centuries,           45788
 Volume 11, by Friedrich Spielhagen, Theodor Storm,
 Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt
 [Subtitle: Masterpieces of German Literature
  Translated Into English]

Zofloya ou le Maure, Tomes 1-4, by Charlotte Dacre                       45787
 [Subtitle: Histoire du XVe si?cle]
 [Language: French]

Their Majesties as I Knew Them, by Xavier Paoli                          45786
 [Subtitle: Personal Reminiscences of the
  Kings and Queens of Europe]
 [Translator: Alexander Teixeira de Mattos]

New York Times Current History: The European War, Vol. 8,                45785
 Pt. 2, No. 1, July 1918, by Various

Gallery of Comicalities, by Robert Cruikshank,                           45784
 George Cruikshank and Robert Seymour
 [Subtitle: Embracing Humorous Sketches]

Katri, by Emil Nervander                                                 45783
 [Subtitle: Kertomus 17 vuosi-sadasta]
 [Language: Finnish]

The Little Brown Jug at Kildare, by Meredith Nicholson                   45782
 [Illustrator: James Montgomery Flagg]

Beaumont & Fletcher's Works (6 through 10), by Francis Beaumont          45781
 and John Fletcher
 [Subtitle: The Queen of Corinth; Bonduca; The Knight of the
  Burning Pestle; Loves Pilgrimage; The Double Marriage]

Beaumont & Fletcher's Works (1 through 5), by Francis Beaumont           45780
 and John Fletcher
 [Subtitle: A Wife for a Month; The Lovers Progress;
  The Pilgrim; The Captain; The Prophetess]

The Washington Historical Quarterly, Volume V, 1914, by Various          45779
 [Editor: Edmond S. Meany]

Minstrelsy of the Scottish Border Volume III of 3, by Walter Scott       45778
 [Subtitle: Consisting of Historical and Romantic Ballads,
  Collected In the Southern Counties of Scotland; With
  a Few Of Modern Date, Founded Upon Local Tradition.
  In Three Volumes. Vol. III]

我想要的只是在一列中插入Winged Wheels in France, by Michael Myers Shoemaker,在另一列中插入45790。然后我就可以将它们添加到我的数据库中。 此外,例如,

The German Classics of the Nineteenth and Twentieth Centuries,           
 Volume 11, by Friedrich Spielhagen, Theodor Storm,
 Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt
 [Subtitle: Masterpieces of German Literature
  Translated Into English]

我想以这种方式插入上面的文字:

The German Classics of the Nineteenth and Twentieth Centuries,           
     Volume 11, by Friedrich Spielhagen, Theodor Storm,
     Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt

表示没有这部分:

[Subtitle: Masterpieces of German Literature
      Translated Into English]

", by"也应该省略,所以我的新数据就是这样的。所以实际上我需要csv中的三列。

1 | Winged Wheels in France | Michael Myers Shoemaker                     |   45790
2 | The German Classics of the Nineteenth and Twentieth Centuries,       
 Volume 11 | Friedrich Spielhagen, Theodor Storm,
 Wilhelm Raabe, Marion D. Learned and Ewald Eiserhardt                    | 45789

请帮助将其插入excel文件并从中创建csv。

谢谢大家。

1 个答案:

答案 0 :(得分:0)

这样做 (未经测试)

$f = file_get_contents('yourtextfile.txt');
$f = preg_replace('/\[(.*?)\]/s','',$f);
$f = str_replace(array("\n", "\r"), '', $f);
file_put_contents('temp.txt',$f);

$file = file('temp.txt');
foreach($file as $key => $line){
    if($line!=null || $line!='')
 {
   mysqli_query($connection,"insert into table1(column1) values('$line')");
 }
}

<强> 修改

$f = file_get_contents('tt.txt');
$f = preg_replace('/\[(.*?)\]/s','',$f);
$keywords = preg_split("/[ ]{15}/", $f);
print_r(array_filter($keywords));

我还不清楚,你的档案中是否有这些号码,或者你刚才在那里提到过它们!