私は、文字列を与えられたオフセットに従ってエンコードされたものに変換するようにPHPで関数を記述しようとしています。例えばPHPのカスタムエンコーディング関数が必要な結果を提供していません
: オフセット2
であり、入力がc
であればオフセットは5
であり、入力がX
であれば、出力も同様e
なり、出力がc
function encode($char,$offset)
{
$char_list = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$_offset = strpos($char_list,$char) + $offset;
if($offset > strlen($char_list)){
$_offset = _$offset - $offset;
}
return $char_list[$_offset];
}
必要な結果である。
encode("a",0) // must returns a
encode("c",5) // must returns h
encode("X",9) // must returns g
あなたの質問は...? – Twinfriends
@Twinfriends、アルゴリズムを修正してください –
[PHP Caesar cipher](https://stackoverflow.com/questions/21177443/php-caesar-cipher)の重複の可能性あり – Profit