Re: String parsing help

From: Date: Wed, 20 Aug 2003 20:32:11 +0000
Subject: Re: String parsing help
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-160238@lists.php.net to get a copy of this message
* Thus wrote Matt Matijevich (matijevich@alliancetechnologies.net): > 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. well if the state is always 2 chars and zip is always at least 5 digits then you can grab the info with preg_match: $string = 'ANCHORAGE AK 99507-1234'; preg_match('/(.*?)\s+([A-Z]{2})\s+(\d{5}[-0-9]*)/', $string, $matches); $city = $matches[1]; $state = $matches[2]; $zip = $matches[3]; Thats should work fine in most cases. Curt -- "I used to think I was indecisive, but now I'm not so sure."

« previous php.general (#160238) next »