City Zip - PHP Online
Form of PHP Sandbox
Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php
in the places where the PHP script should be executed.
Result of php executing
Full code of City Zip.php
- <?php
- $usstatearray = array(
- 'FL'=>'Florida',
- 'PA'=>'Pennsylvania',
- 'MA'=>'Massachusetts');
- function getStateAbbr ($stateArray, $stateName)
- {
- echo "Running getStateAbbr\n";
- foreach ( $stateArray as $stAbbr => $stLong )
- {
- if ( $stateName == $stLong )
- {
- $stateName = $stAbbr;
- }
- }
- return $stateName;
- }
- $cityzip = '19609, Pennsylvania';
- $match = array();
- preg_match("/(?P<zip>\d{5})(?P<state>.*)$/",$cityzip,$match);
- if($match['zip']){
- $clientZipcode = $match['zip'];
- $clientState = trim($match['state']);
- if ( strlen($clientState) > 2 )
- {
- $clientState = getStateAbbr($usstatearray,$clientState);
- }
- if ( empty($clientState) )
- {
- $clientState = "Found in zipcodes database with zip: $clientZipcode";
- }
- $location = $cntyStArray[0]['County'];
- $location = $location.' County';
- }else{
- preg_match("/(?P<city>(.+?)),(?P<state>.*)$/",$cityzip,$match);
- $clientState = trim($match['state']);
- echo strlen($clientState). "\n";
- if ( strlen($clientState) > 2 )
- {
- $clientState = getStateAbbr($usstatearray,$clientState);
- }
- if($match['city']){
- $location = $match['city'];
- }else{
- $location = $cityzip;
- }
- }
- echo "Client State: {$clientState}\n";
- echo "Client Zip: {$clientZipcode}\n";