Computer Science Canada

replacing alphabets.

Author:  Amailer [ Thu Jan 15, 2004 6:35 pm ]
Post subject:  replacing alphabets.

How would i do this?
code:

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


A = n
N = a

and so on.

so if i have this'

uryyb
it should display
hello

Author:  PaddyLong [ Fri Jan 16, 2004 4:49 am ]
Post subject: 

something like this should work...
code:

<?php
function encode ($encodeThis)
{
        $result = $encodeThis;
        $alphabit = ('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',
                     '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');
        $encoding = ('n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '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', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M');

        for ($i = 0; $i < upper ($alphabit); $i++)
        {
                $result = ereg_replace($alphabit[$i], $encoding[$i], $result);
        }
        return ($result);
}
?>

Author:  Amailer [ Sat Jan 17, 2004 2:01 pm ]
Post subject: 

errr nope.
upper() what's that?

also how can you do:
$alphabit = 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', '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');

??

also, i tried soemthing like that...gave me the same result.
thing is that, if the word is
uryyb

the B don't get translated... cuz o = b :S

Author:  PaddyLong [ Sat Jan 17, 2004 7:01 pm ]
Post subject: 

k... this one works, I know what the problem with the old one was too (it would replace a character with something and then when it got to the character the original was replaced with in the array, it would put it back to the original)

upper () was supposed to be count ()

code:

<?php
function encode ($encodeThis)
{
$result = "";
$alphabit = 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', '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');
$encoding = array ('n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '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', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M');

for ($i = 0; $i < strlen ($encodeThis); $i++)
{
$replaced = 0;
        for ($j = 0; $j < count ($alphabit); $j++)
        {
                if ($encodeThis[$i] == $alphabit[$j])
                {
                        $replaced = 1;
                        $result .= $encoding[$j];
                }
        }
        if ($replaced == 0)
        {
                $result .= $encodeThis[$i];

        }
}

return ($result);
}

$test = 'abcdefghigklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ';
print ( encode($test) );
?>

Author:  Amailer [ Sat Jan 17, 2004 8:03 pm ]
Post subject: 

woo, thanks.
Hmm how does this work really? lol

Author:  PaddyLong [ Sun Jan 18, 2004 11:18 am ]
Post subject: 

heh...
k so we have the alphabit array that stores all the characters to be replaced and then the encoding array that stores the characters to replace the ones in alphabit with (the character at index n of alphabit will be replaced with the character at index n of encoding)

then there is a for loop that looks at each character of the string that is being encoded. for each of the letters it goes through the entire alphabit array to determine what index of alphabit the character of the string is. when it finds the character in alphabit it adds to result the character from encoding that is at the index it found and sets replaced to 1 (to say that the character was found in alphabit). if after it looks through the entire alphabit array, replaced is 0, it simply adds to result the original character.

Author:  wtd [ Sat Feb 14, 2004 12:21 am ]
Post subject: 

Ummm... this is actually really easy. Smile

code:
<?
   $input_string = "Hello world!";
   $output_string = str_rot13($input_string);
?>


Feel free to hate me. Wink

Author:  Amailer [ Sat Feb 14, 2004 12:31 am ]
Post subject: 

there is no such function as str_rot13... hmm atleast not on my php (latest non beta version)

And you sure that function is ment to do what that script PaddyLong made does??? lol weird.

Author:  wtd [ Sat Feb 14, 2004 12:39 am ]
Post subject: 

http://ca2.php.net/manual/en/function.str-rot13.php


: