PHP
私が望んでいたほど成功しなかった私の試みは、文字列の長さと特定の文字配置を使用して、州名から略語を抽出しました。おそらく、名前削除のより良いシーケンスが可能です。
function findAbb ($state) {
$first = substr($state, 0, 1);
$last = substr($state, -2,1);
$state = strtolower($state);
if (strlen($state) < 4) {
return strtoupper($state);
}
if (strpos($state, ' ')) { //if it's a space, return the first letter of each word.
$space_index = strpos($state, ' ');
$state = explode(' ', $state);
return strtoupper(substr($state[0], 0, 1) . substr($state[1], 0, 1));
}
if (startsWith($state, 'io')) { //iowa is annoying, get rid of it.
return strtoupper($first . $last);
}
if (startsWith($state, 'w,i')) { //if it starts with a W, return the first 2.
return strtoupper(substr($state, 0, 2));
}
if (strlen($state) < 7 && strpos($state, 'm')===false) { //matches texas, ohio, and utah.
return strtoupper($first . substr($state, -4,1));
}
if (strlen($state) < 7 && substr($state, 0, 1) > 'j' && substr($state, 0, 1) < 'n') { //matches maine, kansas, and hawaii
return strtoupper($first . $last);
}
if (startsWith($state, 'c,d,k,l,p,v,g,h')) { //some unique states
return strtoupper($first . $last);
}
if (strpos($state, 'sk')) {
return strtoupper ('ak');
}
if (startsWith($state, 'k,l', 1)) {
return strtoupper(substr($state, 0, 2));
}
if (startsWith($state, 'n')) {
return strtoupper($first . substr($state, 2, 1));
}
if (startsWith($state, 'n', 2) || startsWith($state, 'z', 3)) { //montana, tennessee, minnesota, and arizona
return strtoupper($first . substr($state, 3, 1));
}
if (startsWith($state, 'm') && ($last == 's') || ($last == 'n')) {
return strtoupper(substr($state, 0, 2));
}
if (strpos($state,'o')) {
return strtoupper($first . 'o');
}
if (strpos($state,'y')) {
return strtoupper($first . 'd');
}
if (strpos($state,'r')) {
return strtoupper($first . 'r');
}
if (strpos($state,'ss')) {
return strtoupper($first . 's');
}
return $state; //otherwise return the name of the state (it was mispelled).
}
function startsWith ($state, $letters, $index = 0) { //takes a comma separated array and finds contents.
$letters = split(',',$letters);
for ($q = 0; $q<count($letters); $q++) {
if (strpos($state,$letters[$q]) === $index) {
return true;
}
}
return false;
}
もちろん、ゴルフもできます。これは私の最初のゴルフの試みなので、洞察力に感謝します。(911)
function t($s){$s=u($s);$f=b($s,0,1);$l=b($s,-2,1);
if(strlen($s)<4)return $s;if(strpos($s,' '))$s=split(' ',$s);
return b($s[0],0,1).b($s[1],0,1);
if(w($s,'IO'))return $f.$l;
if(w($s,'W,I'))return b($s,0,2);
if(strlen($s)<7 && strpos($s,'M')===false)return $f.b($s,-4,1);
if(strlen($s)<7 && b($s,0,1)>'I' && b($s,0,1)<'N')return $f.$l;
if(w($s,'C,D,K,L,P,V,G,H'))return $f.$l;if(strpos($s, 'SK'))return 'AK';
if(w($s,'K,L',1))return b($s,0,2);if(w($s,'N'))return $f.b($s,2,1);
if(w($s,'N',2) || w($s,'Z',3))return $f.b($s,3,1);
if(w($s,'M') && ($l=='S') || ($l=='N'))return b($s,0,2);
if(strpos($s,'O'))return $f.'O';
if(strpos($s,'Y'))return $f.'D';if(strpos($s,'R'))return $f.'R';
if(strpos($s,'SS'))return $f.'S';return $s;}function w($s,$l,$i=0){$l=split(',',$l);
for($q=0;$q<count($l);$q++)if(strpos($s,$l[$q])===$i)return 1;return 0;}
function u($z){return strtoupper($z);}
function b($v,$x,$y){return substr($v,$x,$y);}