状态缩写的PHP脚本无法正常工作 [英] PHP Script for States to Abbreviations Not Working Correctly

查看:85
本文介绍了状态缩写的PHP脚本无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定此脚本有什么问题。当我运行它时,它只会给我缩写的第一个字母。

I'm not sure what's wrong with this script. When I run it it only gives me the first letter of the abbreviation.

我没有写缩写部分,而是在网上找到的。脚本中唯一属于我的部分是GET和Includes。

I didn't write the abbreviation part, I found that online. The only parts of the script that are mine are the GET and Includes.

我将状态变量设置为状态,但仍然得到相同的内容,所以我知道这不是我的代码部分。如果有人知道问题出在哪里,请告诉我。

I've set the state variable to a state and I still get the same thing so I know it's not my portion of the code. If anyone knows what the issue is please let me know.

这里是建议的更新代码。

Here is the updated code as suggested. Still having the same problem.

$state = 'Alabama';

function convert_state($name, $get = 'abbr') {
$states = 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',
'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'
);
if($get == 'name') {
// in this case $name is actually the abbreviation of the state name and you want the full name
$states = array_flip($states);
}

return $states[$name];
}


推荐答案

有一个很好的答案问题已经存在,但是由于OP仍然存在问题,因此这是一个替代解决方案。请注意,数组$ a2s需要完全填充,因为我仅添加了两个状态来保持简短。将以下内容保存到您选择的文件中(例如:state.php):

There's an excellent answer to this question already, but since the OP is still having problems with this, here's an alternate solution. Please note that the array $a2s needs to be fully populated, as I only added two states to keep this short. Save the following to the file of your choice (eg: state.php):

<?php
$state = $_GET['state'];
echo convert_state($state);

function convert_state($key) {
    $a2s = array( 
        'AL'=>'Alabama',
        'CA'=>'California'
    );
    $array = (strlen($key) == 2 ? $a2s : array_flip($a2s));
    return $array[$key];
}
?>

要运行以上命令,请在浏览器中键入以下命令之一(假设文件是state.php):

To run the above, you'd type one of the following in your browser (assuming your file is state.php):

state.php?state=AL
state.php?state=Alabama

该代码旨在以任何一种方式工作。

The code is designed to work either way.

这篇关于状态缩写的PHP脚本无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆