简体   繁体   中英

Php str_ireplace not works

I am trying this functions. And the first - cipher - is working perfectly, I verified it, but when I make decipher it doesnt match the word.
Can someone figure out why doesn't work?

 // Polybius square 1 2 3 4 5 6 1 A Ă Â BCD 2 EFGHI Î 3 JKLMNO 4 PQRS Ș T 5 Ț UVWXY 6 Z . , ? - ! 
function cipher($text) {
$alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');

    $polybios  = array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $output  = str_ireplace($alphabet, $polybios, $text);
    return($output);
   }

function decipher($string) {    
    $alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');

    $polybios  array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $output  = str_ireplace($polybios, $alphabet, $string);   
    return($output);
}

$mesaj='cupcake';
$cipherText = cipher($mesaj);

echo $cipherText;

$decipherText = decipher($cipherText);
echo $decipherText;

I think the problem is that after you've encoded the original string, you're returning a single large number. str_ireplace then doesn't know where one value starts and the other ends to be able to decode it.

If instead, you split your cipher string into chunks of 2 characters (all of your encoded values are 2 digit numbers), and decode them separately, before joining them together again, it works.

function cipher($string) {
    $alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');
    $polybios  = array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $encoded = str_replace($alphabet, $polybios, str_split($string, 1));

    return implode('', $encoded);
}

function decipher($string) {
    $alphabet = array('a','ă','â','b','c','d','e','f','g','h','i','î','j','k','l','m','n','o','p','q','r','s','ș','t','ț','u','v','w','x','y','z','.',',','?','-','!');
    $polybios = array('11','12','13','14','15','16','21','22','23','24','25','26','31','32','33','34','35','36','41','42','43','44','45','46','51','52','53','54','55','56','61','62','63','64','65','66');
    $decoded = str_replace($polybios, $alphabet, str_split($string, 2));

    return implode('', $decoded);
}

$string = 'cupcake';
$cipher = cipher('cupcake');
$decipher = decipher($cipher);

var_dump($string, $cipher, $decipher);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM