使用php从文本文件读取数据并创建数据库

使用php从文本文件读取数据并创建数据库,php,logging,text-files,Php,Logging,Text Files,我有一个文本文件,一个巨大的文件,其中包含一些我需要插入数据库的信息 以下是文本文件的其中一行: 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) C

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

以下是文本文件的其中一行:

 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');
这只是读取文本文件,正如我所说,我需要获取数据并插入数据库,下面是一个示例,我需要如何将行设置为spli:

 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
请帮我解决这个问题。谢谢。

$test='77.242.22.86---[10/Jul/2013:14:07:26+0200]
 $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
 }
“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,类似 Gecko)Chrome/27.0.1453.116 Safari/537.36/alxg-3.1“; 函数标记器($test){ $test=修剪(strtolower($test)); $res=str_replace(“,”,“,”,$test); $res=str_替换(“-”、“”、$res); $res=str_replace(“,”,$res); $result=explode(“,$res”); 对于($i=0;$i

$test=fread($fh);现在将$test变量存储在db中或使用$test=file\u get\u内容('file.txt');您好,回答是thnx,但我需要在不同的行中插入数据。因此我可以使用它们。我的问题是如何从文本中分割数据并将其插入数据库,示例显示在文章的末尾。好的,检查我的答案是否对您有用……这是我写的。$myFile=“mytextt.txt”$fh=fopen($myFile,'r');$test=fread($fh)函数拆分($test){$test=trim(strtolower($test));preg_Split('/(^\p{p}+)|(\p{p}*\s+\p{p}*)|(\p{p}+$)/,$test 1,preg_Split NO_为空);for($i=0;$i