前回はポケベルのコードを変換してみました。
pc.casey.jp » PHPでポケベルコード変換 :
https://pc.casey.jp/archives/2460
今度はケータイ版をやってみます。こちらも CakePHP で動作テストしてみました。
Ex. 2222200055446→こんにちは
※ちなみに、例えば999999(”ろ”の次)の場合、「ら」に戻ります
スポンサードリンク
// kt code to string by pc.casey.jp, GPL
function ktcode_to_string(){
//Configure::write('debug', 2);
// init
if(empty($this->data['Code'])) return;
// test data
// $keys = '22222000554469999999#';
// $matrix= 'kana';
$keys = $this->data['Code'];
$matrix= $this->data['Mode'];
// code load
$codes = $this->_getKtCodes();
// check
$error = null;
if(!ereg('^([0-9\#])+$', $keys)) $error = 'StringError';
if(!array_key_exists($matrix, $codes)) $error = 'MatrixError';
// error report and process stop
if($error){
$this->set('error', $error);
debug($error);
return;
}
// str to array
$keys_array = str_split($keys);
// start init
$str= null;
$r = 0;
$g = $keys_array[0];
for($i=0; $i<count($keys_array); $i++){
// number same or diff
if($g != $keys_array[$i]){
// until now the value of output
$str .= $codes[$matrix][$g][$r-1];
// set new number
$g = $keys_array[$i];
$r = 1;
}else{
// same number
$r++;
// matrix over ?
if(count($codes[$matrix][$g]) <= $r-1) $r = 1;
}
// output if loop last
if($i == count($keys_array)-1){
$str .= $codes[$matrix][$g][$r-1];
}
}
// output
//debug($str);
$this->set('result', $str);
}
function _getKtCodes(){
return $codes = array(
'kana' => array(
'1'=> array('あ', 'い', 'う', 'え', 'お', 'ぁ', 'ぃ', 'ぅ', 'ぇ'),
'2'=> array('か', 'き', 'く', 'け', 'こ'),
'3'=> array('さ', 'し', 'す', 'せ', 'そ'),
'4'=> array('た', 'ち', 'つ', 'て', 'と', 'っ'),
'5'=> array('な', 'に', 'ぬ', 'ね', 'の'),
'6'=> array('は', 'ひ', 'ふ', 'へ', 'ほ'),
'7'=> array('ま', 'み', 'む', 'め', 'も'),
'8'=> array('や', 'ゆ', 'よ', 'ゃ', 'ゅ', 'ょ'),
'9'=> array('ら', 'り', 'る', 'れ', 'ろ'),
'0'=> array('わ', 'を', 'ん', 'ゎ', 'ー'),
'#'=> array('、', '。', '?', '!', '・', ' '),
),
'alpha' => array(
'1'=> array('.', '/', '@', '-', ':', '~', '_', '1'),
'2'=> array('a', 'b', 'c', 'A', 'B', 'C', '2'),
'3'=> array('d', 'e', 'f', 'D', 'E', 'F', '3'),
'4'=> array('g', 'h', 'i', 'G', 'H', 'I', '4'),
'5'=> array('j', 'k', 'l', 'J', 'K', 'L', '5'),
'6'=> array('m', 'n', 'o', 'M', 'N', 'O', '6'),
'7'=> array('p', 'q', 'r', 's', 'P', 'Q', 'R', 'S', '7'),
'8'=> array('t', 'u', 'v', 'T', 'U', 'V', '8'),
'9'=> array('w', 'x', 'y', 'z', 'W', 'X', 'Y', 'Z', '9'),
'0'=> array('0'),
'#'=> array('?', '!', '’', '-', '&', '(', ')', '¥', ' '),
),
);
}

コメント