<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>EoTz.com Web Development Blog &#187; function</title>
	<atom:link href="http://www.eotz.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.eotz.com</link>
	<description>Another web development blog</description>
	<lastBuildDate>Wed, 14 Oct 2009 00:18:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Parsing a United States Location String with PHP</title>
		<link>http://www.eotz.com/2008/07/parsing-location-string-php/</link>
		<comments>http://www.eotz.com/2008/07/parsing-location-string-php/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 01:15:55 +0000</pubDate>
		<dc:creator>chris</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[function]]></category>

		<guid isPermaLink="false">http://www.eotz.com/?p=1</guid>
		<description><![CDATA[So I needed a way to parse/extract a location string into the city and state or zip code for a client&#8217;s site. If you don&#8217;t quite know what I&#8217;m talking about, head over to Superpages and look at the location field. A couple Google searches later, I decided to just go ahead an write my [...]]]></description>
			<content:encoded><![CDATA[<p>So I needed a way to parse/extract a location string into the city and state or zip code for a client&#8217;s site. If you don&#8217;t quite know what I&#8217;m talking about, head over to <a href="http://www.superpages.com/">Superpages</a> and look at the location field. A couple Google searches later, I decided to just go ahead an write my own function. This sounds easy until you think about it. Any of the following inputs need to be able to be parsed.</p>
<ol>
<li>dallas tx</li>
<li>dallas, texas</li>
<li>los angeles california</li>
<li>washington disctrict of columbia</li>
<li>richmond virginia</li>
<li>charleston west virginia</li>
<li>90001</li>
</ol>
<p>This is beyond the scope of a simple regular expression. We can&#8217;t simply use the last word of the input either since the state could be multiple words like in case #4. There&#8217;s no way to determine which word or group of words is the state without testing the string against an array of states. As you can see, cases like #5 and #6 complicate things even further because if we test from the end of the string, virginia could also be west virginia</p>
<p>Here is the function I came up with. Please let me know if you find a bug or condition that cannot be parsed.<span id="more-1"></span></p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1code2'); return false;">View Code</a> PHP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12"><td class="code" id="p1code2"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
** You are free to use and distribute this function provided this notice stays in tact.
**
** parseLocation returns array containing city and state
**
Sample  Usage
$str = 'dallas tx';
$parsed = parseLocation($str);
print 'Your city is '.$parsed['city'].', '.$parsed['state'];
** Original parseLocation function obtained from EoTz.com.
** Last updated: 10/13/2009
**
**/</span>
<span style="color: #000000; font-weight: bold;">function</span> parseLocation<span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$state_list</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'AL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Alabama&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'AK'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Alaska&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'AZ'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Arizona&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'AR'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Arkansas&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'CA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;California&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'CO'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Colorado&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'CT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Connecticut&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'DE'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Delaware&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'DC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;District Of Columbia&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'FL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Florida&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'GA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Georgia&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'HI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Hawaii&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ID'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Idaho&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'IL'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Illinois&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'IN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Indiana&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'IA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Iowa&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'KS'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Kansas&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'KY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Kentucky&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'LA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Louisiana&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ME'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Maine&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MD'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Maryland&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Massachusetts&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Michigan&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Minnesota&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MS'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Mississippi&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MO'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Missouri&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'MT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Montana&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NE'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Nebraska&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NV'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Nevada&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NH'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Hampshire&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NJ'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Jersey&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NM'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New Mexico&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;New York&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'NC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;North Carolina&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'ND'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;North Dakota&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'OH'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Ohio&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'OK'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Oklahoma&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'OR'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Oregon&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'PA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Pennsylvania&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'RI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Rhode Island&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'SC'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;South Carolina&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'SD'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;South Dakota&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'TN'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Tennessee&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'TX'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Texas&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'UT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Utah&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'VT'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Vermont&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'VA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Virginia&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'WA'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Washington&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'WV'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;West Virginia&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'WI'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wisconsin&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">'WY'</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wyoming&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #666666; font-style: italic;">// lets see if the camma forms a valid state and city</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strstr"><span style="color: #990000;">strstr</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span><span style="color: #0000ff;">','</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$parts</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/explode"><span style="color: #990000;">explode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">','</span><span style="color: #339933;">,</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/trim"><span style="color: #990000;">trim</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><a href="http://www.php.net/array_key_exists"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtoupper"><span style="color: #990000;">strtoupper</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$parts</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/preg_split"><span style="color: #990000;">preg_split</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[s,;]+/&quot;</span><span style="color: #339933;">,</span><span style="color: #000088;">$str</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #666666; font-style: italic;">// first see if the last array element is a state abbreviation</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strlen"><span style="color: #990000;">strlen</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">2</span> <span style="color: #339933;">&amp;&amp;</span> <a href="http://www.php.net/array_key_exists"><span style="color: #990000;">array_key_exists</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtoupper"><span style="color: #990000;">strtoupper</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/strtoupper"><span style="color: #990000;">strtoupper</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #666666; font-style: italic;">// since it's not an abbreviation let's see if the last element is the full name of a state</span>
			<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #666666; font-style: italic;">//check if this could be the wrong state (i.e. virginia could be west virginia)</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#91;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// we need at least 2 words left to continue</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
				<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$state</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
					<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
				<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
					<span style="color: #666666; font-style: italic;">// we need at least 2 words left to continue</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/count"><span style="color: #990000;">count</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
					<span style="color: #666666; font-style: italic;">// check if the 3rd word from the end forms a valid state name</span>
					<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_pop"><span style="color: #990000;">array_pop</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$state</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><a href="http://www.php.net/in_array"><span style="color: #990000;">in_array</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
						<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/ucwords"><span style="color: #990000;">ucwords</span></a><span style="color: #009900;">&#40;</span><a href="http://www.php.net/strtolower"><span style="color: #990000;">strtolower</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
						<span style="color: #000088;">$city</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/implode"><span style="color: #990000;">implode</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span><span style="color: #000088;">$parts</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
						<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
					<span style="color: #009900;">&#125;</span>
				<span style="color: #009900;">&#125;</span>
			<span style="color: #009900;">&#125;</span>
			<span style="color: #000088;">$state</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/array_search"><span style="color: #990000;">array_search</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$state</span><span style="color: #339933;">,</span><span style="color: #000088;">$state_list</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">// Here we can query the result against a database to make sure it's valid. Ignore this section if you don't want to check against a database.</span>
<span style="color: #666666; font-style: italic;">/*$sql = 'SELECT `city`,`state` FROM `city_state` WHERE `city`=&quot;'.$city.'&quot; and `state`=&quot;'.$state.'&quot;';
$result = mysql_query($sql);
if(mysql_num_rows($result) &gt; 0){
return array('city'=&gt;mysql_result($result,0,'city'),'state'=&gt;mysql_result($result,0,'state'));
} else {
return false;
}*/</span>
<span style="color: #b1b100;">return</span> <a href="http://www.php.net/array"><span style="color: #990000;">array</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'city'</span><span style="color: #339933;">=</span><span style="color: #000088;">$city</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'state'</span><span style="color: #339933;">=&gt;</span><span style="color: #000088;">$state</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.eotz.com/2008/07/parsing-location-string-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
