Jump to content

Translation


djp1988

Recommended Posts

I want to make a translation page where you type in a message and it get's translated into morse code, how could i do it? I'm thinking of a database but then again maybe just simple php? each letter has it's morse code...

Link to comment
Share on other sites

Make an array for the morse code, gets the decimal value of the character, loop through each character and check if the decimal value is between 0-25 (array keys start at 0 if you use number).

<?php  $morse=array('.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');  $content=strtolower($_POST['content']);  for ($i=0; $i<strlen($content); $i++) {	$dec=ord($content[$i])-97;	if ($dec>=0 && $dec<=25) {	  echo $morse[$dec].' ';	}  }?>

Link to comment
Share on other sites

Hello, thank you that was what i had in mind, but why have you used 'echo' as opposed to 'print' ?What i have done is create a form like so on a page called 'form.php' :

<form action="morse.php" method="post">  <label>content  <textarea name="content" id="content"></textarea>  </label>  <p>	<label>submit	<input type="submit" name="Submit" value="Submit" />	</label>  </p></form>

and the main page ('morse.php') with your code:

<?php  $morse=array('.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');  $content=strtolower($_POST['content']);    for ($i=0; $i<strlen($content); $i++) {	$dec=ord($content[$i])-97;	if ($dec>=0 && $dec<=25) {	  echo $morse[$dec].' ';	}  }?>

but please forgive me, but it doesn't work I know there's something missing, but can't remember, it's been a few months since i ws stuck into my php books

Link to comment
Share on other sites

I just noticed that in your code, you are calculating the string length to find the $decis that correct? because if someone was to enter:"Hello", your strtolower would transform it to "hello" but then your

for ($i=0; $i<strlen($content); $i++) {	$dec=ord($content[$i])-97;

would calculate how many letters (5 for the word hello) is in that string and get the morse code from the array for that decimal, returning a value of " . " (fifth member of the array). and so the person would see a value of " . " which is not morse code for hello, but for " e "

Link to comment
Share on other sites

Why not try messing around with JavaScript??? Then the page wouldn't have to reload... You could use the replace() function and replace all a's with what ever you want, then all b's with what ever you want...or you could use for's and the charAt() function, which takes the 5th character and saves it to a var... If any of those help.

Link to comment
Share on other sites

<?php  $morse=array('.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..'); // Morse code  $content=strtolower($_POST['content']); // makes every letter lower case    for ($i=0; $i<strlen($content); $i++) { // $i adds up all the way to the length of $content (what the user types)	$dec=ord($content[$i])-97; // Get the ASCII value of character and subtract 97 from it, a=97; 97-97=0; a=0.	if ($dec>=0 && $dec<=25) { // Array index starts at 0, a=0; b=1; c=2; ... 	  echo $morse[$dec].' '; // Echo is just what I use to output something. $morse is the array with the morse code as elements, so to retrieve the elements you will use [number] which is the value of the ASCII value-97.	}  }?>

Link to comment
Share on other sites

Okay thanks, I didn't know about the ASCII value, but in order to convert a simple space into 3 spaces i tried this code:

<?php  $morse=array(' ','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');  $content=strtolower($_POST['content']);   for ($i=0; $i<strlen($content); $i++) { 	$dec=ord($content[$i])-32; 	if ($dec>=0 && $dec<=25) { 	  echo $morse[$dec].' ';	}  }?>

as the ASCII value for a space is 32, but it does not work, is there anything wrong?

Link to comment
Share on other sites

ok I've changed

 if ($dec>=0 && $dec<=25)

to

 if ($dec>=0 && $dec<=90)

but I think the problem about the space is '' is == to ' ' or ' ' so there must be a php commande for a space...

Link to comment
Share on other sites

My code now is:

<?php  $morse=array(' / ','-.-.--','.-..-.','','...-..-','','.-...','.----.','-.--.','-.--.-','','.-.-.','--..--','-....-','.-.-.-','-..-.','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','---...','-.-.-.','','-...-','','..--..','.--.-.','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','..--.-','','.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');  $content=strtolower($_POST['content']);   for ($i=0; $i<strlen($content); $i++) { 	$dec=ord($content[$i])-32; 	if ($dec>=0 && $dec<=90) { 	  print '<p>The morse code for ' .$_POST['content'] .' is:</p><br /><p>';	  echo $morse[$dec].' ';	  print '</p>';	}  }?>

but I have a problem with the print & echo commands, it repeats for every letter

Link to comment
Share on other sites

Btw, print == echo

but I have a problem with the print & echo commands, it repeats for every letter
Try placing the structure markup outside the loop
<?php  $morse=array(' / ','-.-.--','.-..-.','','...-..-','','.-...','.----.','-.--.','-.--.-','','.-.-.','--..--','-....-','.-.-.-','-..-.','-----','.----','..---','...--','....-','.....','-....','--...','---..','----.','---...','-.-.-.','','-...-','','..--..','.--.-.','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','','..--.-','','.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..', '.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.', '...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..');  $content=strtolower($_POST['content']);  echo '<p>The morse code for ' .$_POST['content'] .' is:</p><br /><p>';  for ($i=0; $i<strlen($content); $i++) {	$dec=ord($content[$i])-32;	if ($dec>=0 && $dec<=90) {	  echo $morse[$dec].' ';	}  }  echo '</p>';?>

Link to comment
Share on other sites

you may see the result of my project here: http://www.buildingblocksoftheweb.com/morse.php
The spacing between Morse letters are a bit hard to tell apart - what about using a monospaced font such as Courier for the Morse code output, as well as perhaps double spacing between letters?
Link to comment
Share on other sites

thanks... changed...And what if i wanted to do the inverse, writing morse code and translating it into normal writing, could it be possible writting 2 arrays, one in morse and the other in letters, and useig a for (); to select the $i of the matching character or something... could someone help me out?thanks.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...