optimiseIP2Country.php PHP script takes an IP to country CSV data file and optimises it by:
- removing the three-letter country code and country name;
- collapsing sequential entries from the same country; and
- not double-quoting values in the CSV output.
The expected format of the IP to country CSV file is as follows:
(If the input file is not in this format, it is simple enough to change the regular expression that parses the CSV data in the
The output produced by the script is in the following format:
For simplicity, this script terminates if it encounters lines in the input file that are blank or that cannot be parsed (which should not occur in correctly-formatted IP to country CSV files).
Collapsing Sequential Entries
The following example illustrates the collapsing of sequential entries. The start IP address of the second entry is one more than the end IP address of the first entry:
Start IP End IP Country Code 982122496,982155263,JP 982155264,982171647,JP
Therefore, we can merge these two entries into the following single entry:
optimiseIP2Country.php on the command line :
optimiseIP2Country.php INPUT_FILENAME > OUTPUT_FILENAME
optimiseIP2Country.php ip-to-country.csv > optimised-ip-to-country.csv
For a complete description of this optimisation technique, please read Optimising IP Geolocation Data.