CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   How can I parse this text file and save the results in a database? (http://www.codingforums.com/showthread.php?t=283380)

Developr 12-02-2012 01:02 AM

How can I parse this text file and save the results in a database?
 
I want to take this text file:
http://phpir.com/user/files/text/lexicon.txt

and in each row I want a value for the word, and the identifier (the 2 or 3 letters beside that word).

How can I separate the two though if there's no delimeter? Is there a way to represent the new line as the delimeter instead?

mlseim 12-02-2012 01:46 AM

Is this a one-time operation, or will you be doing this over and over again in the future?

And is the junk stuff at the beginning of the file needed, or do you only want the lines that are letters?

Developr 12-02-2012 02:16 AM

one time, and junk not needed.

I also noticed some words have two identifiers ex:
word nn nnp

I guess I could store a second in another col?

mlseim 12-02-2012 03:04 AM

1) I would use notepad+ to edit the text file ... cleaning it up.
Example,
change all / characters to spaces
change all , to spaces
change all | to spaces
change all \ to spaces
etc.

2) Get it cleaned up so that you only have the special characters you allow, like & _ . -

3) So now you'll have a text file, each line has the characters that what you want.
Some lines will have multiple spaces in between some words, but that's OK.

4) Now you'll use PHP to clean up the extra spaces.

PHP Code:

<?php
// Put your file into an array ... each line is one array element.
$data=file("lexicon.txt");

// Connect to your MySQL database here.

// Loop through the array of lines, clean up extra spaces, and insert into table ...
foreach($data as $line){
// Remove extra spaces so there is only one space.
$line preg_replace('/\s+/'' '$line);
// Explode the line into separate elements
   
$parts=explode(" ",$line);
   
// Now you have some parts to write into your database table
   // Example: Average NNP JJ NN
   // $parts[0] = Average
   // $parts[1] = NNP
   // $parts[2] = JJ
   // $parts[3] = NN

// insert $parts[0] for sure, and then the other columns if they exist.
}

?>


That's sort of what I would do. Using Notepad+ to edit files, PHP, HTML, CSS is the best text editor.


.


All times are GMT +1. The time now is 05:51 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.