A simple "translator"

General discussions related to php

Moderators: macek, egami, gesf

Post Reply
peterachutha
New php-forum User
New php-forum User
Posts: 10
Joined: Sat May 31, 2014 10:35 pm

Re: A simple "translator"

Post by 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>

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest