Created
September 19, 2016 17:23
-
-
Save jwieder/adec4244d17bc753d7c8701581fe2e4d to your computer and use it in GitHub Desktop.
PHP script to convert a text file of two letter state abbreviations into full text names. You can replace the $us_state_abbrev array with just about any one-dimensional array to convert whatever you want (countries, etc). You can also easily change this so that full state names are converted to two-state codes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Replace list.txt with the name of your file. The file should only include a list of two-letter state abbreviations, one on each line. | |
* Results printed to STDIO, so use pipes to dump to a file if you want to save the results or add a function to do the same thing (I was | |
* using this for CLI use which is why a function isnt in here already) | |
* | |
* Josh Wieder | |
* https://consulting.joshwieder.net | |
*/ | |
$txtFile = "list.txt"; | |
$us_state_abbrev = array( | |
'Alabama' => 'AL', | |
'Alaska' => 'AK', | |
'Arizona' => 'AZ', | |
'Arkansas' => 'AR', | |
'California' => 'CA', | |
'Colorado' => 'CO', | |
'Connecticut' => 'CT', | |
'Delaware' => 'DE', | |
'Florida' => 'FL', | |
'Georgia' => 'GA', | |
'Hawaii' => 'HI', | |
'Idaho' => 'ID', | |
'Illinois' => 'IL', | |
'Indiana' => 'IN', | |
'Iowa' => 'IA', | |
'Kansas' => 'KS', | |
'Kentucky' => 'KY', | |
'Louisiana' => 'LA', | |
'Maine' => 'ME', | |
'Maryland' => 'MD', | |
'Massachusetts' => 'MA', | |
'Michigan' => 'MI', | |
'Minnesota' => 'MN', | |
'Mississippi' => 'MS', | |
'Missouri' => 'MO', | |
'Montana' => 'MT', | |
'Nebraska' => 'NE', | |
'Nevada' => 'NV', | |
'New Hampshire' => 'NH', | |
'New Jersey' => 'NJ', | |
'New Mexico' => 'NM', | |
'New York' => 'NY', | |
'North Carolina' => 'NC', | |
'North Dakota' => 'ND', | |
'North Carolina' => 'NC', | |
'North Dakota' => 'ND', | |
'Ohio' => 'OH', | |
'Oklahoma' => 'OK', | |
'Oregon' => 'OR', | |
'Pennsylvania' => 'PA', | |
'Rhode Island' => 'RI', | |
'South Carolina' => 'SC', | |
'South Dakota' => 'SD', | |
'Tennessee' => 'TN', | |
'Texas' => 'TX', | |
'Utah' => 'UT', | |
'Vermont' => 'VT', | |
'Virginia' => 'VA', | |
'Washington' => 'WA', | |
'West Virginia' => 'WV', | |
'Wisconsin' => 'WI', | |
'Wyoming' => 'WY', | |
); | |
$csvRowArray = readCSV($txtFile); | |
function readCSV($txtFile){ | |
if (file_exists($txtFile)) { | |
$file_handle = fopen($txtFile, 'r'); | |
while (!feof($file_handle) ) { | |
$line_of_text[] = fgetcsv($file_handle, 1024); | |
} | |
fclose($file_handle); | |
$line_of_text = array_values($line_of_text); | |
$line_of_text = array_filter( $line_of_text ); | |
return $line_of_text; | |
} else { | |
echo "\nText file does not exist."; | |
die; | |
} | |
} | |
ob_start(); | |
ob_start(); | |
foreach($csvRowArray as $key => $csvRow) { | |
$key = array_search($csvRow[0], $us_state_abbrev); | |
print_r ($key); | |
echo "\n"; | |
} | |
exit(0); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment