Or (untested):

$String = 'NORTH LITTLE ROCK  AR  72118-5227';
$Bits = split(strrev($String), '[[:space:]]+', 3);
$Zip = strrev($Bits[0]);
$State = strrev($Bits[1]);
$City = strrev($Bits[2]);

...could also use preg_split()

- steve

At 4:44 PM -0400 8/20/03, "CPT John W. Holmes" <[EMAIL PROTECTED]> wrote:
From: "Jonatan Pugliese." <[EMAIL PROTECTED]>
 From: "Matt Matijevich" <[EMAIL PROTECTED]>
 > I have have a string that I need to split into 3 different variables:
 > City,State, and Zip.  Here is a couple examples of the strings I need to
 > parse:
 >
 > ANCHORAGE  AK  99507-6420
 > JUNEAU  AK  99801
> > NORTH LITTLE ROCK AR 72118-5227
 >
 > Does anyone have an idea how I could slit this into the appropriate
 > variables, maybe some kind of regular expression?  I cant use the space
 > character to split the data because some of the city names have spaces
 > in them.

$vector=split( " ", $string, );


$City=$vector[0]; $State=$vector[1]; $Zip=$vector[2]:

Umm, no. then you'll have $City = "North", $State = "Little" and $Zip = "Rock" with the last example.

The following works:

<?php

$str = "NORTH LITTLE ROCK  AR  72118-5227
JUNEAU  AK  99801
ANCHORAGE  AK  99507-6420
NORTH CARO  MI  48732
";

preg_match_all('/^(.*)([a-z]{2})\s+([0-9]{5}(-[0-9]{4})?)/im',$str,$matches)
;

$count = count($matches[1]);

echo $count . ' addresses found:<br />';

for($x=0;$x<$count;$x++)
{
    $city = trim($matches[1][$x]);
    $state = trim($matches[2][$x]);
    $zip = trim($matches[3][$x]);
    echo "City: $city, State: $state, ZIP: $zip<br />";
}

?>

---John Holmes...


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


--
+------------------------------------------------------------------------+
| Steve Edberg                                      [EMAIL PROTECTED] |
| University of California, Davis                          (530)754-9127 |
| Programming/Database/SysAdmin               http://pgfsun.ucdavis.edu/ |
+------------------------------------------------------------------------+
| [EMAIL PROTECTED]: 1001 Work units on 23 oct 2002                              |
| 3.152 years CPU time, 3.142 years SETI user... and STILL no aliens...  |
+------------------------------------------------------------------------+

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to