djp1988 Posted December 27, 2007 Share Posted December 27, 2007 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 More sharing options...
zppblood Posted December 27, 2007 Share Posted December 27, 2007 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 More sharing options...
djp1988 Posted December 29, 2007 Author Share Posted December 29, 2007 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 More sharing options...
djp1988 Posted December 29, 2007 Author Share Posted December 29, 2007 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 More sharing options...
djp1988 Posted December 29, 2007 Author Share Posted December 29, 2007 Possibly best to use this: $morse=array('a'=>'.-', 'b'=>'-...', ............ then haveing a foreach commande ? Link to comment Share on other sites More sharing options...
clonetrooper9494 Posted December 29, 2007 Share Posted December 29, 2007 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 More sharing options...
zppblood Posted December 30, 2007 Share Posted December 30, 2007 <?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 More sharing options...
djp1988 Posted December 30, 2007 Author Share Posted December 30, 2007 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 More sharing options...
djp1988 Posted December 30, 2007 Author Share Posted December 30, 2007 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 More sharing options...
djp1988 Posted December 30, 2007 Author Share Posted December 30, 2007 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 More sharing options...
Synook Posted December 31, 2007 Share Posted December 31, 2007 Btw, print == echo but I have a problem with the print & echo commands, it repeats for every letterTry 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 More sharing options...
djp1988 Posted December 31, 2007 Author Share Posted December 31, 2007 you may see the result of my project here: http://www.buildingblocksoftheweb.com/morse.phpbtw what do you think of the site as the design, idea etc... does the right column appear under and to the left? Link to comment Share on other sites More sharing options...
zppblood Posted December 31, 2007 Share Posted December 31, 2007 Use PHP instead of Javascript to get the date. People who don't have Javascript on will not see it.Something like date('l, F d, Y');http://us3.php.net/manual/en/function.date.phpScroll down to see all the ways to retrieve date/time with the date function and letters. Link to comment Share on other sites More sharing options...
Synook Posted January 1, 2008 Share Posted January 1, 2008 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 More sharing options...
djp1988 Posted January 4, 2008 Author Share Posted January 4, 2008 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.