State to Name/Abbreviation/Code Conversion Function


/ Published in: PHP
Save to your folder(s)

State to Name/Abbreviation/Code Conversion Function


Copy this code and paste it in your HTML
  1. /* Convert State Name/Abbrev/Code Function */
  2. function convertState($strInput, $strFormat='name', $blnDefaultToInput=false) {
  3. $arrStates = array(
  4. array('code'=>'01', 'abbrev'=>'AL', 'name'=>'Alabama'),
  5. array('code'=>'02', 'abbrev'=>'AK', 'name'=>'Alaska'),
  6. array('code'=>'04', 'abbrev'=>'AZ', 'name'=>'Arizona'),
  7. array('code'=>'05', 'abbrev'=>'AR', 'name'=>'Arkansas'),
  8. array('code'=>'06', 'abbrev'=>'CA', 'name'=>'California'),
  9. array('code'=>'08', 'abbrev'=>'CO', 'name'=>'Colorado'),
  10. array('code'=>'09', 'abbrev'=>'CT', 'name'=>'Connecticut'),
  11. array('code'=>'10', 'abbrev'=>'DE', 'name'=>'Delaware'),
  12. array('code'=>'11', 'abbrev'=>'DC', 'name'=>'District of Columbia'),
  13. array('code'=>'11', 'abbrev'=>'DC', 'name'=>'Washington DC'),
  14. array('code'=>'11', 'abbrev'=>'DC', 'name'=>'Washington D.C.'),
  15. array('code'=>'12', 'abbrev'=>'FL', 'name'=>'Florida'),
  16. array('code'=>'13', 'abbrev'=>'GA', 'name'=>'Georgia'),
  17. array('code'=>'15', 'abbrev'=>'HI', 'name'=>'Hawaii'),
  18. array('code'=>'16', 'abbrev'=>'ID', 'name'=>'Idaho'),
  19. array('code'=>'17', 'abbrev'=>'IL', 'name'=>'Illinois'),
  20. array('code'=>'18', 'abbrev'=>'IN', 'name'=>'Indiana'),
  21. array('code'=>'19', 'abbrev'=>'IA', 'name'=>'Iowa'),
  22. array('code'=>'20', 'abbrev'=>'KS', 'name'=>'Kansas'),
  23. array('code'=>'21', 'abbrev'=>'KY', 'name'=>'Kentucky'),
  24. array('code'=>'22', 'abbrev'=>'LA', 'name'=>'Louisiana'),
  25. array('code'=>'23', 'abbrev'=>'ME', 'name'=>'Maine'),
  26. array('code'=>'24', 'abbrev'=>'MD', 'name'=>'Maryland'),
  27. array('code'=>'25', 'abbrev'=>'MA', 'name'=>'Massachusetts'),
  28. array('code'=>'26', 'abbrev'=>'MI', 'name'=>'Michigan'),
  29. array('code'=>'27', 'abbrev'=>'MN', 'name'=>'Minnesota'),
  30. array('code'=>'28', 'abbrev'=>'MS', 'name'=>'Mississippi'),
  31. array('code'=>'29', 'abbrev'=>'MO', 'name'=>'Missouri'),
  32. array('code'=>'30', 'abbrev'=>'MT', 'name'=>'Montana'),
  33. array('code'=>'31', 'abbrev'=>'NE', 'name'=>'Nebraska'),
  34. array('code'=>'32', 'abbrev'=>'NV', 'name'=>'Nevada'),
  35. array('code'=>'33', 'abbrev'=>'NH', 'name'=>'New Hampshire'),
  36. array('code'=>'34', 'abbrev'=>'NJ', 'name'=>'New Jersey'),
  37. array('code'=>'35', 'abbrev'=>'NM', 'name'=>'New Mexico'),
  38. array('code'=>'36', 'abbrev'=>'NY', 'name'=>'New York'),
  39. array('code'=>'37', 'abbrev'=>'NC', 'name'=>'North Carolina'),
  40. array('code'=>'38', 'abbrev'=>'ND', 'name'=>'North Dakota'),
  41. array('code'=>'39', 'abbrev'=>'OH', 'name'=>'Ohio'),
  42. array('code'=>'40', 'abbrev'=>'OK', 'name'=>'Oklahoma'),
  43. array('code'=>'41', 'abbrev'=>'OR', 'name'=>'Oregon'),
  44. array('code'=>'42', 'abbrev'=>'PA', 'name'=>'Pennsylvania'),
  45. array('code'=>'44', 'abbrev'=>'RI', 'name'=>'Rhode Island'),
  46. array('code'=>'45', 'abbrev'=>'SC', 'name'=>'South Carolina'),
  47. array('code'=>'46', 'abbrev'=>'SD', 'name'=>'South Dakota'),
  48. array('code'=>'47', 'abbrev'=>'TN', 'name'=>'Tennessee'),
  49. array('code'=>'48', 'abbrev'=>'TX', 'name'=>'Texas'),
  50. array('code'=>'49', 'abbrev'=>'UT', 'name'=>'Utah'),
  51. array('code'=>'50', 'abbrev'=>'VT', 'name'=>'Vermont'),
  52. array('code'=>'51', 'abbrev'=>'VA', 'name'=>'Virginia'),
  53. array('code'=>'53', 'abbrev'=>'WA', 'name'=>'Washington'),
  54. array('code'=>'54', 'abbrev'=>'WV', 'name'=>'West Virginia'),
  55. array('code'=>'55', 'abbrev'=>'WI', 'name'=>'Wisconsin'),
  56. array('code'=>'56', 'abbrev'=>'WY', 'name'=>'Wyoming'),
  57. array('code'=>'60', 'abbrev'=>'AS', 'name'=>'American Samoa'),
  58. array('code'=>'66', 'abbrev'=>'GU', 'name'=>'Guam'),
  59. array('code'=>'69', 'abbrev'=>'MP', 'name'=>'Northern Mariana Islands'),
  60. array('code'=>'72', 'abbrev'=>'PR', 'name'=>'Puerto Rico'),
  61. array('code'=>'78', 'abbrev'=>'VI', 'name'=>'Virgin Islands'),
  62. array('code'=>'64', 'abbrev'=>'FM', 'name'=>'Federated States of Micronesia'),
  63. array('code'=>'68', 'abbrev'=>'MH', 'name'=>'Marshall Islands'),
  64. array('code'=>'70', 'abbrev'=>'PW', 'name'=>'Palau')
  65. );
  66.  
  67. $strOutput = ($blnDefaultToInput) ? $strInput : '';
  68. $strFormat = strtolower(trim($strFormat));
  69.  
  70. foreach ($arrStates as $arrState) {
  71. foreach ($arrState as $strValue) {
  72. if (strtolower($strValue) == strtolower(trim($strInput))) {
  73. if ($strFormat == 'code') {
  74. $strOutput = $arrState['code'];
  75. }
  76. else if ($strFormat == 'abbrev') {
  77. $strOutput = $arrState['abbrev'];
  78. }
  79. else {
  80. $strOutput = $arrState['name'];
  81. }
  82. break;
  83. }
  84. }
  85. }
  86.  
  87. return $strOutput;
  88. }

URL: http://concept211.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.