AvinDE
AvinDE

Reputation: 25

Read data from text file with php and create database

I have a text file, a huge one that contains some information's that I need to insert into a database.

Here is the one of the rows of the text file:

 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"

I am using some php functions like:

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

This only read the text file, as I said, I need to get the data and insert into the database, here is an example how I need the row to be 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

Please help me to resolve this problem. Thank you.

Upvotes: 0

Views: 389

Answers (1)

Bharu
Bharu

Reputation: 191

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

Upvotes: 1

Related Questions