繁体   English   中英

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

[英]Read data from text file with php and create database

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

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

 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

请帮助我解决此问题。 谢谢。

 $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
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM