<meta charset="UTF-8">
<?php
// get parameter
$str = isset($_GET["str"]) : "";
$shift = isset($_GET["shift"]) ? intval($_GET["shift"]) : 3;
if ($str != "")convert($str, $shift);
$str_ = htmlentities($str, ENT_QUOTES);
// display form
echo <<< EOS
<form>
character line: <input name="str" value="$str_"><br>
shift: <input name="shift" value="$shift"><br>
<input type=="submit" value="change">
</form>
EOS;
function makeTable($shift){
$ch1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$shift = $shift % strlen($ch1);
$ch2 = substr($ch1, $shift).substr($ch1, 0, $shift);
$table = [];
for ($i = 0; $i < strlen($ch1); $i++){
$c1 = substr($ch1, $i, 1);
$c2 = substr($ch2, $i, 1);
$table[$c2] = $c1;
}
return $table;
}
function convert($str, $shift){
if (empty($_GET["str"])) return;
$table = makeTable($shift);
$res = "";
for ($i = 0; $i < strlen($str); $i++){
$c = substr($str, $i, 1);
$res .= isset($table[$c]) ? $table[$c] : $c;
}
$str = htmlentities($str, ENT_QUOTES);
$res = htmlentities($res, ENT_QUOTES);
echo "<div>before transfer: $str</div>";
echo "<div>after transfer: $res</div><hr>";
}