Re: Exploding Zip Codes! (Using PHP's explode function)
| From: | Bogdan Stancescu | Date: | Mon, 12 Aug 2002 20:49:16 +0000 |
| Subject: | Re: Exploding Zip Codes! (Using PHP's explode function) | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-112203@lists.php.net to get a copy of this message | ||
If that's the exact string you have (which I doubt) then your code should look something like
<?
$string=" | 36310 ABBEVILLE |
| 35440 ABERNANT |
| 35005 ADAMSVILLE |
| 35540 ADDISON |
| 35006 ADGER |
| 35441 AKRON |";
$nl="\n";
$array=explode(" |$nl | ",$string);
while (list(,$zip)=each($array)) {
$zip=explode(" ",$zip);
$zipcode=intval($zip[0]); // The zip code
$zipcity=$zip[1]; // The corresponding city
}
?>
Notes on the code above:
1. Your string most probably doesn't look like that, in which case you'll have to adapt the code to it;
2. You'll have to do some custom changes to the first and last entry because they will contain some extra stuff (the first zip will start with " | " - and therefore evaluate to 0, and the last city will end with " |" which is not right;
3. Depending on the source OS, you may have to change $nl to "\r\n" instead of simply "\n".
Anyway, that should be the general idea.
Bogdan
Keith Posehn wrote:
I have a huge block of zip codes I need to seperate into their constituent parts for a query of a database. The block looks like this (in part): | 36310 ABBEVILLE | | 35440 ABERNANT | | 35005 ADAMSVILLE | | 35540 ADDISON | | 35006 ADGER | | 35441 AKRON | I need to use eplode (probably) to seperate this all out into two arrays, one of codes, the other of names--presumably named $zips[] and $cities[] with the rest of the data dumped. I have gone and looked at the various functions docs at php.net, but they haven't really helped to answer my question as much as I would like. Any information is appreciated. Thanks