I teho the following problem in PHP
I need to remove the characters from a string, and then insert in the same position. I'm currently doing this, but I'm open to suggestions.
I have an initial string: te12te
I remove the numbers from this string and store the character and position in two arrays:
$stringInicial = "te12te";
//Processo de remoção resulta em:
$arrayCaracteres = ["1", "2"];
$arrayPosicao = [2, 3];
$stringInicial = "tete";
//As letras da string passam por um processo e são alteradas seguindo uma lógica
$stringInicial = "vivi";
//Preciso agora inserir os caracteres na mesma posição
Given this information, I need to enter the removed characters at the beginning, in the same position. That is, the result must be vi12vi
EDIT: The procedure performed is the PlayFair cipher, follow the code that does this:
for ($i = 0; $i < strlen($plainText); $i += 2) {
//compute coords
$x0 = array_search($plainText[$i], $this->matrix) % 5;
$y0 = intval(array_search($plainText[$i], $this->matrix) / 5);
$x1 = array_search($plainText[$i + 1], $this->matrix) % 5;
$y1 = intval(array_search($plainText[$i + 1], $this->matrix) / 5);
if ($y0 == $y1) {
//same line
$encrypted .= $this->matrix[5 * $y0 + (($x0 + 1) % 5)];
$encrypted .= $this->matrix[5 * $y1 + (($x1 + 1) % 5)];
} elseif ($x0 == $x1) {
//same column
$encrypted .= $this->matrix[5 * (($y0 + 1) % 5) + $x0];
$encrypted .= $this->matrix[5 * (($y1 + 1) % 5) + $x1];
} else {
//line and column are different
$encrypted .= $this->matrix[(5 * $y0 + $x1)];
$encrypted .= $this->matrix[(5 * $y1 + $x0)];
}
}