0

我有一个文本文件,一个巨大的文件,其中包含一些我需要插入数据库的信息。

这是文本文件的其中一行:

 77.242.22.86 - - [10/Jul/2013:14:07:26 +0200] "GET /web/img/theimage.jpg HTTP/1.1" 304 - "http://www.mywebiste.com/web/"
 "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like
 Gecko) Chrome/27.0.1453.116 Safari/537.36 AlexaToolbar/alxg-3.1"

我正在使用一些 php 函数,例如:

$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');

这只读取文本文件,正如我所说,我需要获取数据并插入到数据库中,这里是我需要如何分割行的示例:

 77.242.22.86
 [10/Jul/2013:14:07:26 +0200] 
 GET 
 /web/img/theimage.jpg 
 HTTP/1.1
 304
 http://www.mywebiste.com/web/ 
 Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like
 Gecko) Chrome/27.0.1453.116 Safari/537.36 AlexaToolbar/alxg-3.1

请帮我解决这个问题。谢谢你。

4

1 回答 1

1
 $test ='77.242.22.86 - - [10/Jul/2013:14:07:26 +0200]
 "GET /web/img/theimage.jpg HTTP/1.1" 304 - "http://www.mywebiste.com/web/"
 "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like
 Gecko) Chrome/27.0.1453.116 Safari/537.36 AlexaToolbar/alxg-3.1"';    
 function tokenizer($test) {
 $test = trim(strtolower($test));
 $res=  str_replace(",","",$test);
 $res=  str_replace("-","",$res);
 $res=  str_replace(' "',"",$res);
 $result=  explode(" ", $res);
 for ($i = 0; $i < count($result); $i++) {
 $result[$i] = trim($result[$i]);
 echo $result[$i]; ?>

 <hr/> <?php

 }
 return $result; // contains the single words
 }
于 2013-07-11T10:29:13.947 回答