Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

A simple "translator"

General discussions related to php

Moderators: macek, egami, gesf

A simple "translator"

Postby Galaniitoluodda » Tue Apr 15, 2014 11:17 am

Hello. I'm making a simple "translator" between Norwegian and "Sibsøf". Sibsøf is a language where A is S, B is N, C is V etc. See on these keys your keyboard so you will understand how it does work.

I have a small problem with the translator PHP. When i choose "From Norwegian to Sibsøf" and write "a", the result should be "s". But the result is "d". The translator first translate "a" to "s", but after that do the translator translate again. Then will the translator translate "s" to "d". I will block the second translation. In this example, the result should be "s". I hope someone can help me! :)

The code:
Code: Select all
<html>
<head>
<title>
Sibsøf-norsk-sibsøf-oversetter
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
<?php include 'header.php'; ?>
</head>
<body>
<center>
<table border="0" style="width:350px;">
    <form action="" method="post">
    <tr><td><center><input type="radio" value="sibsoftilnorsk" name="spraak"><br><font face="Verdana">Sibsøf &#8594; norsk</font></input></center></td><br />
    <td><center><input type="radio" value="norsktilsibsof" name="spraak"><br><font face="Verdana">Norsk &#8594; sibsøf</font></input></center></td></tr><br />
</table>
    <textarea name="melding" cols="50" rows="15"></textarea><br />
    <input type="submit" value="Oversett" name="oversett" />
    </form>
</center>
     
<?php
$tekst = $_POST["melding"];
$tekst3 = trim($tekst);
$tekst2 = strtoupper($tekst3);

if(isset($_POST["oversett"]))
{
    //////////sibsoftilnorsk///////////////////////
    $bokstaver1 = array("s","n","v","f","r","g","h","j","o","k","l","ø",";","m","p","å","w","t","d","y","i","b","e","c","u","x","*","æ","^","S","N","V","F","R","G","H","J","O","K","L","Ø",";","M","P","Å","W","T","D","Y","I","B","E","C","U","X","*","Æ","^");
    $plassering1 = 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","Æ","Ø","Å");
 
    //////////norsktilsibsof///////////////////////
    $bokstaver2 = 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","Æ","Ø","Å");
    $plassering2 = array("s","n","v","f","r","g","h","j","o","k","l","ø",";","m","p","å","w","t","d","y","i","b","e","c","u","x","*","æ","^","S","N","V","F","R","G","H","J","O","K","L","Ø",";","M","P","Å","W","T","D","Y","I","B","E","C","U","X","*","Æ","^");
 
    /////////////////////////////////////////////////////////////////
    ////////sibsoftilnorsk melding///////////////
    /////////////////////////////////////////////////////////////////
    if($_POST["spraak"] == "sibsoftilnorsk")
    {         
        echo "<center><br><br><b>Norsk-versjon (original):</b><br />";
        echo $tekst3;
         
        $tekst3 = str_replace($bokstaver1,$plassering1,$tekst3);
         
        echo "<br /><br /><b>Norsk-versjon (oversettelse):</b><br />";
        echo $tekst3 . "<br /><br />";
        echo '</center>';
    }
   
    /////////////////////////////////////////////////////////////////
    ////////norsktilsibsof melding///////////////
    /////////////////////////////////////////////////////////////////
    if($_POST["spraak"] == "norsktilsibsof")
    {
        echo "<center><br><br><b>Norsk-versjon (original):</b><br />";
        echo $tekst3;
         
        $tekst3 = str_replace($bokstaver2,$plassering2,$tekst3);
         
        echo "<br /><br /><b>Sibsøf-versjon (oversettelse):</b><br />";
        echo $tekst3 . "<br /><br />";
        echo '</center>';
    }
}
?>

</body>
</html>
Galaniitoluodda
New php-forum User
New php-forum User
 
Posts: 1
Joined: Tue Apr 15, 2014 11:10 am

Re: A simple "translator"

Postby peterachutha » Sun Jun 01, 2014 4:53 am

I went through the code and rearranged to see if it was code related. Then I found out that, it looks like character by character replacement does not work with str_replace(). Probably something to do with a string and an array of characters not treated the same.

So I modified your code with char replacing functions that returned a string. And I dropped some characters which were in your set but not on my keyboard. Also added a space character into your character set so that it will not be replaced with anything else. Here is the code, (sorry its a bit messy) :-

<?php

//////////sibsoftilnorsk///////////////////////
$bokstaver1 = array('s','n','v','f','r','g','h','j','o','k','l',';',',','m','p','[','w','t','d','y','i','b','e','c','u','x','S','N','V','F','R','G','H','J','O','K','L',':','<','M','P','{','W','T','D','Y','I','B','E','C','U','X','a','z', 'q');
$plassering1 = 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','Æ','Ø','Å');

//////////norsktilsibsof///////////////////////
$bokstaver2 = 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',':','?','}');
$plassering2 = array('s','n','v','f','r','g','h','j','o','k','l','ø',';','m','p','å','w','t','d','y','i','b','e','c','u','x','*','æ','^','S','N','V','F','R','G','H','J','O','K','L','Ø',';','M','P','Å','W','T','D','Y','I','B','E','C','U','X','*','Æ','^');

$alpha[0]= array(' ','s','n','v','f','r','g','h','j','o','k','l',';',',','m','p','[','w','t','d','y','i','b','e','c','u','x','S','N','V','F','R','G','H','J','O','K','L',':','<','M','P','{','W','T','D','Y','I','B','E','C','U','X','a','z', 'q');
$alpha[1] = 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',':','?','}');

$maxchar = count( $alpha[0] );

function strreplace( $a, $s , $maxchar){
$maxa = $maxchar;
$maxs = strlen( $s );
$r = '';
for( $i=0; $i<$maxs; $i++){
for( $j=0; $j<$maxa; $j++){
if( $s[$i] == $a[1][$j] ){
$r = $r.$a[0][$j];
}
}
}
return( $r );
}
function revstrreplace( $a, $s, $maxchar ){
$maxa = $maxchar;
$maxs = strlen( $s );
$r ='';
for( $i=0; $i<$maxs; $i++){
for( $j=0; $j<$maxa; $j++){
if( $s[$i] == $a[0][$j] ){
$r = $r.$a[1][$j];
}
}
}
return( $r );
}
?>

<html>
<head>
<title>
Sibsøf-norsk-sibsøf-oversetter
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="style.css" rel="stylesheet" type="text/css">
<?php //include 'header.php'; ?>
</head>
<body>
<?php
$tekst = $_POST["melding"];
$tekst3 = trim($tekst);
$tekst2 = strtoupper($tekst3);


//if(isset($_POST["oversett"]))
if( $_POST['oversett'] == 'Submit' )
{
/////////////////////////////////////////////////////////////////
////////sibsoftilnorsk melding///////////////
/////////////////////////////////////////////////////////////////
if($_POST["spraak"] == "sibsoftilnorsk")
{
echo '<br>sibsoftilnors';
echo "<center><br><br><b>Norsk-versjon (original):</b><br />";
echo $tekst3;

//$tekst4 = str_replace($bokstaver1,$bokstaver2,$tekst3);
$tekst4 = strreplace( $alpha, $tekst3, $maxchar );
echo "<br /><br /><b>Norsk-versjon (oversettelse):</b><br />";
echo $tekst4 . "<br /><br />";
echo '</center>';
}
else
/////////////////////////////////////////////////////////////////
////////norsktilsibsof melding///////////////
/////////////////////////////////////////////////////////////////
if($_POST["spraak"] == "norsktilsibsof")
{
echo '<br>norsktilsibsof';
echo "<center><br><br><b>Norsk-versjon (original):</b><br />";
echo $tekst3;

//$tekst4 = str_replace($bokstaver2,$bokstaver1,$tekst3);
$tekst4 = revstrreplace( $alpha, $tekst3 , $maxchar);
echo "<br /><br /><b>Sibsøf-versjon (oversettelse):</b><br />";
echo $tekst4 . "<br /><br />";
echo '</center>';
}
}
?>
<center>
<table border="0" style="width:350px;">
<form action="" method="post">
<tr><td><center><input type="radio" value="sibsoftilnorsk" name="spraak"><br><font face="Verdana">Sibsøf &#8594; norsk</font></input></center></td><br />
<td><center><input type="radio" value="norsktilsibsof" name="spraak"><br><font face="Verdana">Norsk &#8594; sibsøf</font></input></center></td></tr><br />
</table>
<textarea name="melding" cols="50" rows="15"><?php echo $tekst; ?></textarea><br />
<input type="submit" value="Submit" name="oversett" />
</form>
</center>

<?php


?>

</body>
</html>
peterachutha
New php-forum User
New php-forum User
 
Posts: 10
Joined: Sat May 31, 2014 10:35 pm


Return to PHP General

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron