Remove and insert characters in the same position

1

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)];
    }
 }
    
asked by anonymous 22.07.2017 / 01:38

2 answers

1

Considering that the position is each byte and that they may not be together you can use two mb_substr .

foreach($arrayPosicao as $index => $posicao){

    $stringAtePosicao = mb_substr($stringInicial, 0, $posicao, '8bit');
    $stringDepoisPosicao = mb_substr($stringInicial, $posicao, null, '8bit');

    $stringInicial = $stringAtePosicao . $arrayCaracteres[$index] . $stringDepoisPosicao;

}

You can pack this into a single line, but I think it gets clearer this way. First we get the whole string to the position, then add the content and get the rest of the string to the end.

Result:

vi12vi

This also works in several overrides, if necessary, for example:

$stringInicial = "te12te3abc4";

$arrayCaracteres = ['1', '2', '3', '4'];
$arrayPosicao = [2, 3, 6, 10];
$stringInicial = "tete";

$stringInicial = "viviabc";


foreach($arrayPosicao as $index => $posicao){

    $stringChange  = mb_substr($stringInicial, 0, $posicao, '8bit');
    $stringChange .= $arrayCaracteres[$index];
    $stringChange .= mb_substr($stringInicial, $posicao, null, '8bit');

    $stringInicial = $stringChange;

}

echo $stringInicial;

Result:

vi12vi3abc4
    
22.07.2017 / 03:56
0

Considering that you will keep the string the same size, you can use substr_replace to do this:

foreach($arrayCaracteres as $k=>$value) {
    substr_replace($stringInicial, $value, 0, $arrayPosicao[$k]);
}
  • For each position found, make the change during the foreach with $ value and the array index of positions ($ k);
  • The 0 indicates whether the character count is from the first position;
22.07.2017 / 02:22