views:

60

answers:

3

I have a very big .txt file with our clients order and I need to move it in a mysql database . However I don't know what kind of regex to use as the information is not very different .

-----------------------
4046904


KKKKKKKKKKK
Laura Meyer
MassMutual Life Insurance
153 Vadnais Street

Chicopee, MA 01020
US
413-744-5452
[email protected]...


KKKKKKKKKKK
373074210772222 02/12 6213 NA
-----------------------
4046907


KKKKKKKKKKK
Venkat Talladivedula

6105 West 68th Street

Tulsa, OK 74131
US
9184472611
venkat.talladivedula...


KKKKKKKKKKK
373022121440000 06/11 9344 NA
-----------------------

I tried something but I couldn't even extract the name ... here is a sample of my effort with no success


$htmlContent = file_get_contents("orders.txt");

//print_r($htmlContent);

$pattern = "/KKKKKKKKKKK(.*)\n/s";
preg_match_all($pattern, $htmlContent, $matches);
print_r($matches);
$name = $matches[1][0];
echo $name;

+4  A: 

You may want to avoid regexes for something like this. Since the data is clearly organized by line, you could repeatedly read lines with fgets() and parse the data that way.

Sam Dufel
Seconded. This looks more like a job for `fgets()` or `file()`, and then walking through each line to extract the single records.
Pekka
A: 

You could read this file with regex, but it may be quite complicated create a regex that could read all fields.

I recommend that you read this file line by line, and parse each one, detecting which kind of data it contains.

mdrg
A: 

As you know exactly where your data is (i.e. which line its on) why not just get it that way?

i.e. something like

$htmlContent = file_get_contents("orders.txt");

$arrayofclients = explode("-----------------------",$htmlContent);
$newlinesep = "\r\n";
for($i = 0;i < count($arrayofclients);$i++)
{
$temp = explode($newlinesep,$arrayofclients[i]);
$idnum = $temp[0];
$name = $temp[4];
$houseandstreet = $temp[6];
//etc
}

or simply read the file line by line using fgets() - something like:

$i = 0;$j = 0;
$file = fopen("orders.txt","r");
$clients = [];
while ($line = fgets($ffile) )
{
    if(line != false)
    {
        $i++;
        switch($i)
        {
        case 2:
            $clients[$j]["idnum"] = $line;
            break;
        case 6:
            $clients[$j]["name"] = $line;
            break;
        //add more cases here for each line up to:
        case 18:
            $j++;
            $i = 0;
            break;
        //there are 18 lines per client if i counted right, so increment $j and reset $i.
        }
    }
}
fclose ($f);

You could use regex's, but they are a bit awkward for this situation.

Nico

Nico Burns