Jump to content

[SOLVED] Translator... what to do?


JP128

Recommended Posts

Ok, lets say that I wanted to build a letter translator. I would need something that would take, lets say for 'a' and to make it 'ka', and for 'b' to make it 'tu'. I have a whole list that my little sister uses to do it, and I wanted to see if I could create a translator in PHP. I was thinking of using eregi_replace(), but I ran into a problem. If it finds the letter again that it translated it from, it would re-translate.

 

For example, Johnny would be 'Zumoritotofu'. With my translator it comes out to be 'Zidomoarihiarihiichimoeichimochimofu.'

 

I know why it does it, I just don't know syntactically what to use to do it properly.

 

here is the key:

 

A - ka 
B - tu  
C - mi  
D - te  
E - ku  
F - lu  
G - ji 
H - ri  
I - ki  
J - zu  
K - me  
L - ta  
M - rin 
N - to 
O - mo  
P - no  
Q - ke  
R- shi  
S - ari 
T -chi 
U - do  
V - ru  
W - mei 
X - na  
Y - fu  
Z - zi 

 

and here is my code to do it.

 

<?php
if(isset($_POST['message']) && $_POST['toFrom'] == "to"){

echo translate_into($_POST['message']);

} else if(isset($_POST['message']) && $_POST['toFrom'] == "from"){

echo translate_from($_POST['message']);

} 
//Prints a message box.
echo "<br><br>

<form method='POST' name='translator'>

<textarea rows=5 cols=25 name='message'></textarea><br>

Translate to:<input type='radio' value='to' name='toFrom'><br>

Translate from:<input type='radio' value='from' name='toFrom'><br>

<input type='submit' value='Translate!' name='translate'><br>

</form>
";




function translate_into($string){

$newString = $string;
$newString = eregi_replace('a','ka',$newString);
$newString = eregi_replace('b','tu',$newString);
$newString = eregi_replace('c','mi',$newString);
$newString = eregi_replace('d','te',$newString);
$newString = eregi_replace('e','ku',$newString);
$newString = eregi_replace('f','lu',$newString);
$newString = eregi_replace('g','ji',$newString);
$newString = eregi_replace('h','ri',$newString);
$newString = eregi_replace('i','ki',$newString);
$newString = eregi_replace('j','zu',$newString);
$newString = eregi_replace('k','me',$newString);
$newString = eregi_replace('l','ta',$newString);
$newString = eregi_replace('m','rin',$newString);
$newString = eregi_replace('n','to',$newString);
$newString = eregi_replace('o','mo',$newString);
$newString = eregi_replace('p','no',$newString);
$newString = eregi_replace('q','ke',$newString);
$newString = eregi_replace('r','shi',$newString);
$newString = eregi_replace('s','ari',$newString);
$newString = eregi_replace('t','chi',$newString);
$newString = eregi_replace('u','do',$newString);
$newString = eregi_replace('v','ru',$newString);
$newString = eregi_replace('w','mei',$newString);
$newString = eregi_replace('x','na',$newString);
$newString = eregi_replace('y','fu',$newString);
$newString = eregi_replace('z','zi',$newString);
return $newString;

}
function translate_from($string){

$newString = $string;
$newString = eregi_replace('ka','a',$newString);
$newString = eregi_replace('tu','b',$newString);
$newString = eregi_replace('mi','c',$newString);
$newString = eregi_replace('te','d',$newString);
$newString = eregi_replace('ku','e',$newString);
$newString = eregi_replace('lu','f',$newString);
$newString = eregi_replace('ji','g',$newString);
$newString = eregi_replace('ri','h',$newString);
$newString = eregi_replace('ki','i',$newString);
$newString = eregi_replace('zu','j',$newString);
$newString = eregi_replace('me','k',$newString);
$newString = eregi_replace('ta','l',$newString);
$newString = eregi_replace('rin','m',$newString);
$newString = eregi_replace('to','n',$newString);
$newString = eregi_replace('mo','o',$newString);
$newString = eregi_replace('no','p',$newString);
$newString = eregi_replace('ke','q',$newString);
$newString = eregi_replace('shi','r',$newString);
$newString = eregi_replace('ari','s',$newString);
$newString = eregi_replace('chi','t',$newString);
$newString = eregi_replace('do','u',$newString);
$newString = eregi_replace('ru','v',$newString);
$newString = eregi_replace('mei','w',$newString);
$newString = eregi_replace('na','x',$newString);
$newString = eregi_replace('fu','y',$newString);
$newString = eregi_replace('zi','z',$newString);
return $newString;

}
?> 

Link to comment
Share on other sites

Translating into is easy, you just need to make a temp copy of a string and return that. eg;

 

<?php

 function translate_into($string) {
   $key_vals = array(
     'a' => 'ka',
     'b' => 'tu',
     'c' => 'mi',
     'd' => 'te', 
     'e' => 'ku',
     ' ' => ' '
   );

   $tmp = '';

   for($i=0;$i<=strlen($string);$i++) {
     $tmp .= $key_vals[$string{$i}];
   }

   return $tmp;

 }

 echo translate_into('ab cde ab');

?>

 

However, turning it back is going to be alot more complex.

Link to comment
Share on other sites

I've used numbers as a transition phase to stop the decoder from altering parts that have already been decoded:

<?php
function decode($message)
{
$letters = array('ka','tu','mi','te','ku','lu','ji','ri','ki','zu','me','ta','rin','to','mo','no','ke','shi','ari','chi','do','ru','mei','na','fu','zi',' ');
$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',' ');
for($i=0;$i < count($letters);$i++)
{
	$message = eregi_replace($letters[$i],$i.'.',$message);
}

$tmp = '';

foreach(explode('.',$message) as $t)
{
	$tmp .= $alphabet[$t];
}
return $tmp;
}

function encode($message)
{
$letters = array('ka','tu','mi','te','ku','lu','ji','ri','ki','zu','me','ta','rin','to','mo','no','ke','shi','ari','chi','do','ru','mei','na','fu','zi',' ');
$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',' ');
for($i=0;$i < count($alphabet);$i++)
{
	$message = eregi_replace($alphabet[$i],$i.'.',$message);
}

$tmp = '';

foreach(explode('.',$message) as $t)
{
	$tmp .= $letters[$t];
}
return $tmp;
}

Link to comment
Share on other sites

Oops, I noticed that because some of the codes overlap (ari/rin/ri), some of the letters get mistaken.  I've reversed the loop and all works fine now:

<?php
function decode($message)
{
$letters = array('ka','tu','mi','te','ku','lu','ji','ri','ki','zu','me','ta','rin','to','mo','no','ke','shi','ari','chi','do','ru','mei','na','fu','zi',' ');
$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',' ');
for($i = count($letters) -1;$i >= 0;$i--)
{
	$message = eregi_replace($letters[$i],$i.".",$message);
}

$tmp = "";

foreach(explode('.',$message) as $t)
{
	$tmp .= $alphabet[$t];
}
return $tmp;
}

function encode($message)
{
$letters = array('ka','tu','mi','te','ku','lu','ji','ri','ki','zu','me','ta','rin','to','mo','no','ke','shi','ari','chi','do','ru','mei','na','fu','zi',' ');
$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',' ');
for($i = count($letters) -1;$i >= 0;$i--)
{
	$message = eregi_replace($alphabet[$i],$i.".",$message);
}

$tmp = "";

foreach(explode('.',$message) as $t)
{
	$tmp .= $letters[$t];
}
return $tmp;
}
?>

Link to comment
Share on other sites

We've given you ideas. I suggest you work on this yourself. As I said, decoding will become quite complex. I would think If I was getting paid for this job, I might quote a few hours work. Do you expect us to write it for you?

Link to comment
Share on other sites

I am not asking for it to be written, i was just saying that it didn't work. I just needed some ideas because I didn't know 'what' to do... I knew what had to be done, but not how to do it. I think tho, with this, I can try to create on like this.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.