Page 1 of 1

Web based alphabet transcoder

Posted: Fri Apr 22, 2005 3:13 pm
by ShineDesign
Hello all,

I am new to PHP, and don't know how I would get started completing this task. I need to develop a web based alphabet transcoder. Basically, the user would input english text into a text box, click a "Transcode" button and it would output the result. The PHP would need to replace the alphabet letters with other pre-defined letters. These letters are:

Code: Select all

A=Y
B=C
C=F
D=Z
E=I
F=L
G=J
H=S
I=X
J=T
K=P
L=N
M=T
N=M
O=u
P=B
Q=D
r=E
S=G
T=H
u=K
V=O
W=Q
X=r
Y=V
Z=A
The script would also need a reverse-coding function, so if the user paste's the already transcoded text into a seperate text box, then clicks "Reverse Transcode", it would output the legible english text. (I would assume the code for this would be the same as the first option, just re-specifying the alphabet letters to replace)

Any help would be greatly appreciated, thanks!

Edit note: this post is driving me insane. I don't know why it keeps automatically changing the letters with words above, but if you see the word "are", just think of the letter that sounds like that. Same with "you".

Jcart | added

Code: Select all

tags to remove word filters[/color][/size]

Posted: Fri Apr 22, 2005 3:40 pm
by evilmonkey
Look into str_replace().

Posted: Fri Apr 22, 2005 4:33 pm
by ShineDesign
Thanks for the help! Ok, so I've got this so far:
(Again, this is really my first time coding PHP...so bear with me. I'm more than willing to do the leg work myself, if I could be pointed in the right direction)

Code: Select all

<?php
function createCode {
   $code = array(&quote;A&quote;, &quote;B&quote;, &quote;C&quote;, &quote;D&quote;, &quote;E&quote;, &quote;F&quote;,
     &quote;G&quote;, &quote;H&quote;, &quote;I&quote;, &quote;J&quote;, &quote;K&quote;, &quote;L&quote;, &quote;M&quote;, &quote;N&quote;, &quote;O&quote;,
     &quote;P&quote;, &quote;Q&quote;, &quote;R&quote;, &quote;S&quote;, &quote;T&quote;, &quote;U&quote;, &quote;V&quote;, &quote;W&quote;, &quote;X&quote;, &quote;Y&quote;, &quote;Z&quote;);
   $code_replace = array(&quote;Y&quote;, &quote;C&quote;, &quote;F&quote;, &quote;Z&quote;, &quote;I&quote;, &quote;L&quote;,
     &quote;J&quote;, &quote;S&quote;, &quote;X&quote;, &quote;T&quote;, &quote;P&quote;, &quote;N&quote;, &quote;W&quote;, &quote;M&quote;, &quote;U&quote;,
     &quote;B&quote;, &quote;D&quote;, &quote;E&quote;, &quote;G&quote;, &quote;H&quote;, &quote;K&quote;, &quote;O&quote;, &quote;Q&quote;, &quote;R&quote;, &quote;V&quote;, &quote;A&quote;);

   $lowercasecode = array(&quote;a&quote;, &quote;b&quote;, &quote;c&quote;, &quote;d&quote;, &quote;e&quote;, &quote;f&quote;,
     &quote;f&quote;, &quote;g&quote;, &quote;e&quote;, &quote;j&quote;, &quote;k&quote;, &quote;l&quote;, &quote;m&quote;, &quote;n&quote;, &quote;o&quote;,
     &quote;p&quote;, &quote;q&quote;, &quote;r&quote;, &quote;s&quote;, &quote;t&quote;, &quote;u&quote;, &quote;v&quote;, &quote;w&quote;, &quote;x&quote;, &quote;y&quote;, &quote;z&quote;);
   $lowercasecode_replace = array(&quote;y&quote;, &quote;c&quote;, &quote;f&quote;, &quote;z&quote;, &quote;i&quote;, &quote;l&quote;,
     &quote;j&quote;, &quote;s&quote;, &quote;x&quote;, &quote;t&quote;, &quote;p&quote;, &quote;n&quote;, &quote;w&quote;, &quote;m&quote;, &quote;u&quote;,
     &quote;b&quote;, &quote;d&quote;, &quote;e&quote;, &quote;g&quote;, &quote;h&quote;, &quote;k&quote;, &quote;o&quote;, &quote;q&quote;, &quote;r&quote;, &quote;v&quote;, &quote;a&quote;);
}
And here is the page I want it to go in:

Code: Select all

<html>
<head>
<title>Transcoding Utility</title>
<meta http-equiv=&quote;Content-Type&quote; content=&quote;text/html; charset=iso-8859-1&quote;>
</head>

<body bgcolor=&quote;#666666&quote;>
<table width=&quote;75%&quote; height=&quote;401&quote; border=&quote;0&quote;>
  <tr>
    <td width=&quote;49%&quote; height=&quote;332&quote; align=&quote;left&quote; valign=&quote;top&quote;> 
      <p><font color=&quote;#FFFFFF&quote; size=&quote;4&quote; face=&quote;Verdana, Arial, Helvetica, sans-serif&quote;>Transcode</font></p>
<p><textarea name=&quote;textbox1&quote; cols=&quote;50&quote; rows=&quote;10&quote; id=&quote;textbox1&quote;></textarea>
<br>
<input name=&quote;transcode&quote; type=&quote;submit&quote; id=&quote;transcode&quote; value=&quote;Transcode&quote;>
</p>
	
	</td>
    <td width=&quote;51%&quote; align=&quote;right&quote; valign=&quote;top&quote;>
	<p><font color=&quote;#FFFFFF&quote; size=&quote;4&quote; face=&quote;Verdana, Arial, Helvetica, sans-serif&quote;>Reverse 
        Transcode</font></p>
<p>
        <textarea name=&quote;textbox2&quote; cols=&quote;50&quote; rows=&quote;10&quote; id=&quote;textbox2&quote;></textarea>
<br>
        <input name=&quote;rev_transcode&quote; type=&quote;submit&quote; id=&quote;rev_transcode&quote; value=&quote;Reverse Transcode&quote;>
</p>
	
	</td>
  </tr>
</table>
</body>
</html>
What should I do next to implement the PHP, and to echo the transcode?
(The php code I put above would only be for the first "transcode" area on the page, I can just duplicate that for the second "Reverse transcode".)

Edit note: added in the php a lowercase array in case the user inputs lowercase letters.

Posted: Fri Apr 22, 2005 9:39 pm
by evilmonkey
Alright, the idea is to have two files, an html file and a PHP file (as you move on in PHP, you will learn to combine the two). In the html, your form action should point to your php file. For clarity sake, let's called it process.php:

Code: Select all

&lt;form action=&quote;process.php&quote;&gt;
That will submit the variable textbox2 (not good to name variables like that, it's from your own code, for clarity sake, I will call the variable "transcode").

That does it for the html code, let's move to the PHP. In PHP, you want to access your transcode variable, and you can do it using the $_POST[] superglobal array. In this case, your variable is $_POST['transcode']. This means that if you call

Code: Select all

<?php 
echo $_POST['transcode'];
?>
your output will be whatever you input on the HTML page.

After you have the variable, you want to break it apart into letters. I'm not exactly sure how to do that, but you will have to manipulate an array. After you have an array of letters, all you have to do is transcode them. Use strlen() on the original variable ($_POST['transcode']) to get the amount of letters, then use a for loop to cycle. Here's what I mean:

Code: Select all

<?php
//remeber, $_POST['transcode'] is your string variable
//break it into an array called $letters. I'm not sure how to do this.
$num_letters = strlen($_POST['transcode']); //this tells you the amount of letters in your variable
for ($i=0;$i<=$num_letters;$i++){ //this cycles it as many times as there are letters
   //assign $letters[$i] its conjugate letter
}//end the loop
?>
This is pseudocode, in no way real. This is to make you understand the logic. As far as the actual syntax goes, there's the PHP manual. If you're still stuck, post back with what you've got. :)

Good luck!

Posted: Sat Apr 23, 2005 3:40 am
by onion2k

Code: Select all

<?php

$string = strtoupper($_POST['string']);
$from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$to =   "YCFZILJSXTPNWMUBDEGHKOQRVA";

if ($_POST['submit'] == "Forward") {
	$string = strtr($string,$from,$to);
} else {
	$string = strtr($string,$to,$from);
}

?>
<form action="index.php" method="POST">
<textarea name="string"><?php echo $string; ?></textarea>
<input type="submit" name="submit" value="Forward">
<input type="submit" name="submit" value="Reverse">
</form>
strtr() is slower than str_replace, but I think it makes tidier code.

Posted: Sat Apr 23, 2005 11:50 am
by ShineDesign
Thanks guys, you rock!

Ok, so I've got the page up and running:

http://www.shinedesign.org/other/transcode

However, if you notice when you code the "english into seeker" and have an apostrophe, it adds one forward slashes before the apostraphe. Ex:

"Don't" becomes "Don/'t"

Any ideas why?

Thanks again for the help, you guys are awesome.

Posted: Sat Apr 23, 2005 11:52 am
by Chris Corbyn
It's actually adding backslashes....

stripslashes() will fix that ;-)

Posted: Sat Apr 23, 2005 1:58 pm
by ShineDesign
Thanks for the tip!

So I added:

Code: Select all

$string = stripslashes($string);
to the first textfield transcoder, so it looks like:

Code: Select all

<?php $string = strtoupper($_POST&#1111;'string']);
$from = &quote;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quote;;
$to =   &quote;YCFZILJSXTPNWMUBDEGHKOQRVA&quote;; 

if ($_POST&#1111;'submit'] == &quote;Forward&quote;) {    
  $string = strtr($string,$from,$to);
   } else {    
    $string = strtr($string,$to,$from);
	
$string = stripslashes($string);	
	}
?>
and it works great.

However,
I added:

Code: Select all

$newstring = stripslashes($revstring);
to the second textfield transcoder, so it looks like:

Code: Select all

<?php $newstring = strtoupper($_POST&#1111;'string']);
$from = &quote;YCFZILJSXTPNWMUBDEGHKOQRVA&quote;;
$to =   &quote;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quote;; 

if ($_POST&#1111;'submit'] == &quote;Forward&quote;) {    
  $newstring = strtr($revstring,$from,$to);
   } else {    
    $newstring = strtr($revstring,$to,$from);

$newstring = stripslashes($revstring);	
}
?>
and now it doesn't transcode back to english anymore. Any ideas why?

Edit Note:
I put the

Code: Select all

$string = stripslashes($string);
in a different place of the script:

Code: Select all

<?php $newstring = strtoupper($_POST&#1111;'string']);
$from = &quote;YCFZILJSXTPNWMUBDEGHKOQRVA&quote;;
$to =   &quote;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quote;; 
$string = stripslashes($string);
if ($_POST&#1111;'submit'] == &quote;Forward&quote;) {    
  $newstring = strtr($revstring,$from,$to);
   } else {    
    $newstring = strtr($revstring,$to,$from);

	
}
?>
and at least it now transcodes back to english. It still adds the backslashes though. I changed the first textfield transcoder so that the stripslashes is in the same place as well, and it works fine. Any ideas?

Posted: Sun Apr 24, 2005 4:57 am
by onion2k

Code: Select all

<?php
 
$string = strtoupper($_POST['string']);
$from = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$to =   "YCFZILJSXTPNWMUBDEGHKOQRVA";
 
if ($_POST['submit'] == "Forward") {
    $string = strtr($string,$from,$to);
} else {
    $string = strtr($string,$to,$from);
}
 
?>
<form action="index.php" method="POST">
<textarea name="string"><?php echo stripslashes($string); ?></textarea>
<input type="submit" name="submit" value="Forward">
<input type="submit" name="submit" value="Reverse">
</form>

Posted: Sun Apr 24, 2005 10:50 am
by bobsta63
Thats well cool mate, I like the idea, but can you make it so you can set the cypher? :lol:

Posted: Sun Apr 24, 2005 4:15 pm
by ShineDesign
Thanks a bunch!

Works excellent now.


Now does anybody know how to make the textfields transparent???

Posted: Mon Apr 25, 2005 3:18 am
by n00b Saibot