0


I'm working the webpage that I have some text like this:
AA,BB,CC,A1,B2,C3
And I want some code to replace the the word (like AA) to the string that saved in variable for example this is my php variable:

<?php
    $var1 = "A"; //For AA
    $var2 = "B"; //For BB 
    $var3 = "C"; //For CC
    $var4 = "1"; //For A1
    $var5 = "2"; //For B2
    $var6 = "3"; //For C3
?>

And the text AA,BB,CC,A1,B2,B3 will be like this:
ABC123
How can I do this with php?

In summery I need a decryption algorithm for a cypher text of AA,BB,CC,A1,B2,C3 to be converted as plan text of ABC123.

3
  • You better should use an array to store different String values. Then you can easily change them by using: myarray[0] = "xxxx" Commented Aug 28, 2014 at 17:52
  • Just for clarification, you want to remove the first letter of each text between the commas? Commented Aug 28, 2014 at 17:52
  • You may want to give a real world example of what you want. I can see already everyone has a different way of interpreting this request. I read it as you want a regex that replaces all consecutive same letters with the singular of that letter, so AAAAAAAAA would turn into A the same way AA turns into A. Commented Aug 28, 2014 at 17:58

4 Answers 4

3

Full working script with sample encryption catalog given here.

<?php
$encript = array(
'a'=>'GG', 
'b'=>'HH',  
'c'=>'II',

'A'=>'DD',  
'B'=>'EE',
'B'=>'FF',

'1'=>'AA', 
'2'=>'BB',
'3'=>'CC');


function decrypt($str,$encript)
{
    $decript = array_flip($encript);
    $str_arr = explode(",",$str);
    $dec = "";
    foreach($str_arr as $val)
    {
        $dec .= $decript[strtoupper(trim($val))];
    }
    return $dec;
}

function encrypt($str,$encript)
{ 
    $str_arr = str_split($str);
    $dec = "";
    foreach($str_arr as $val)
    {
        $dec .= $encript[trim($val)].",";
    }
    return $dec;
}


$cypher = "AA,BB,DD,CC,EE,FF,GG,HH";
$text = "Ab1Ca";

echo decrypt($cypher,$encript);
echo "<br/>";
echo encrypt($text,$encript);
?>
  • First we have to create the mapping for each character as associative array.
  • In decrypt function first we have to flip the array.
  • Then have to loop through each encoding string and build the real text from flipped array.
  • In encryption split the string as single letter array.
  • Loop through the array and build encryption using the mapping array.

The working out put available at following url: http://sugunan.net/demo/str1.php

Sign up to request clarification or add additional context in comments.

8 Comments

Tnx, But I have a question I have about 62 variable can I use array? Could you add the full code in demo?
Yes, you can use array to handle. See the modified code at following url: sugunan.net/demo/str1.php
But I want to know how you set the AA to a I'm looking for this
can you put your full string then I can make the function.
actually it's one of my friends encryption so I can't tell you But I show some of them: notepad.cc/spoclaido37 and it Continue to z and A-Z I want to create some translator of these code
|
2

Assuming the text is stored in $string, try this:

$vars = array(
    'AA' => 'A',
    'BB' => 'B',
    'CC' => 'C',
    'A1' => '1',
    'B2' => '2',
    'C3' => '3',
);

$string = str_replace(array_keys($vars), array_values($vars), $string);

Also heed the warning from PHP's docs on str_replace:

Replacement order gotcha

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

2 Comments

Skip the foreach and do str_replace(array_keys($vars), array_values($vars), $string);
@dave thanks. Thanks to dognose as well (but I can't tag two people at once)
0

Try this

$str = 'AA,BB,CC,A1,B2,C3';
$arr = explode(',', $str);
$newStr = '';
foreach($arr as $val){
    $newStr .= substr($val,1,1);
}
echo $newStr;

Output

ABC123

1 Comment

Op says: Replacement should be saved in a variable - not the second character of the string. (Works with the example cause of coincidence)
0

If you don't want to change your strings try this

$count = 0;
while(true){
    $var = "var".++$count;
    if($$var){
        $$var = ${$var}[1];
    }
    else break;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.