user3065191
user3065191

Reputation: 145

PHP plus RegularExpression parse

I've regex a file until I got this (example):

December
Street etc
500-111 New York
USA
January
Street etc
111-999 Russia
Russia

As you can see, the items are splitted in 4 to 4, the first line is the 'Month', the second the 'Street', the third 'Postal code', and fourth 'Country'.

That said, I have 1500 lines of this, to be more precisely, 1504.

How can I loop all the items and parse the items by category? What I've tried so far:

$file_handle = fopen("file.txt", "rb");
while (!feof($file_handle) ) {

    $line_of_text = fgets($file_handle);
    $parts = explode("\n", $line_of_text); // Doesn't split by line

    echo $parts[0] . "<br/>"; // Gives the 1504 lines

}
fclose($file_handle);

I thought about using the multiples. etc..but I don't know how to implement that, since I can have multiple numbers equals, like multiple of 2 and 3, have the number 6..probably I could put that into an array and verify if already splitted that.

Or this is to much, and there are much better options?

Upvotes: 1

Views: 44

Answers (2)

Reactgular
Reactgular

Reputation: 54741

If you know the file contents exactly groups of 4 lines, then this should do the trick. 1504 lines is not very many. So I'd just load it all at once with file_get_contents. fopen is just extra work with no benefit.

$lines = explode("\n",str_replace("\r","",file_get_contents($path)));

for(int $i=0; $i < count($lines); $i=$i+4)
{
    $month = $lines[$i];
    $street = $lines[$i+1];
    $address = $lines[$i+2];
    $country = $lines[$i+3];
}

Upvotes: 1

Phil Perry
Phil Perry

Reputation: 2130

If it's always groups of 4 lines, how about

$file_handle = fopen("file.txt", "rb");
while (!feof($file_handle) ) {

    for ($i=0; $i<4; $i++) {
        $line_of_text = fgets($file_handle);

        switch($i) {
            case 0: // do something with month
                    break;
            case 1: // do something with street address
                    break;
            case 2: // do something with postal code and state/region/province
                    break;
            case 3: // do something with country
                    break;
        }
    }

    // done with a group
}
fclose($file_handle);

I take it that the 'b' mode is because you're working on Windows with maybe a Linux-source file? That could explain why you're not seeing newlines in your original code.

Upvotes: 2

Related Questions