cjburkey01 Posted October 29, 2013 Share Posted October 29, 2013 So I made this code: <?php if(isset($_POST['box'], $_POST['choose'])) { if($_POST['choose'] == "to") { $eng = 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'); $udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v'); $convertedLanguage = str_replace($eng, $udc, $_POST['box']); } else if($_POST['choose'] == "from") { //Convert text from UDC } } ?> <html> <head> <title>UDC Converting To/From</title> </head> <body> <center> <h1>UDC To/From</h1> <form action="" method="post"> <?php echo $convertedLanguage; ?> <textarea name="box" rows="10" cols="50"></textarea><br /> <label>To UDC</label> <input type="radio" name="choose" value="to" checked="checked" /><br /> <label>From UDC</label> <input type="radio" name="choose" value="from" /><br /> <input type="submit" value="Change" /> </form> </center> </body> </html> And it works for most letters, but some don't like the 'e', turns to 'o', then back to an 'e' If I put in 'Test' I get 'peqp'. Like it should be, but the 'e' should be an 'o' To me, it seems as if it running through the e twice. I don't know why? is there a better method? Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 29, 2013 Share Posted October 29, 2013 http://us3.php.net/manual/en/function.str-replace.php See example 2, that indicates this issue. Maybe try preg_replace Quote Link to comment Share on other sites More sharing options...
cjburkey01 Posted October 29, 2013 Author Share Posted October 29, 2013 So I could do $eng = 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'); $udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v'); $convertedLanguage = preg_replace($eng, $udc, $_POST['box']); Or do I have to put the slashes in from and behind each part in the array? Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 29, 2013 Share Posted October 29, 2013 Probably with and an "i" to make sure it does them all. '/a/i' (I'm not on a machine with a server to test) Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 29, 2013 Share Posted October 29, 2013 You may have the same issue with preg_replace: http://us1.php.net/manual/en/function.preg-replace.php Note: When using arrays with pattern and replacement, the keys are processed in the order they appear in the array. This is not necessarily the same as the numerical index order. If you use indexes to identify which pattern should be replaced by which replacement, you should perform a ksort() on each array prior to calling preg_replace(). You may have to resort to doing a loop to iterate over each character. Quote Link to comment Share on other sites More sharing options...
cjburkey01 Posted October 29, 2013 Author Share Posted October 29, 2013 OK, so like $eng = array('/a/i', '/b/i', '/c/i', '/d/i', '/e/i', '/f/i', '/g/i', '/h/i', '/i/i', '/j/i', '/k/i' ,'/l/i' ,'/m/i', '/n/i', '/o/i', '/p/i', '/q/i', '/r/i', '/s/i', '/t/i', '/u/i', '/v/i', '/w/i', '/x/i', '/y/i', '/z/i'); Quote Link to comment Share on other sites More sharing options...
mentalist Posted October 29, 2013 Share Posted October 29, 2013 If (when) you go the route of iterating through the string you could use modulus (sometimes called modulo, but is % in PHP and many other languages) This commonly known as a shift cipher or Caesar cipher: http://en.wikipedia.org/wiki/Caesar_cipher Chars are generally stored as numbers, you can use the following to convert back and forth: http://us2.php.net/manual/en/function.chr.php http://us2.php.net/manual/en/function.ord.php but one issue here is that a isn't 0 it's 97, whereas A is 65, so it'd may need offsetting after the mod (if < 97 then + 97) Quote Link to comment Share on other sites More sharing options...
Solution ignace Posted October 29, 2013 Solution Share Posted October 29, 2013 You need strtr which does not have this problem since it does single-byte translations. $eng = 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'); $udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v'); echo strtr('test', array_combine($eng, $udc)); Quote Link to comment Share on other sites More sharing options...
cjburkey01 Posted October 29, 2013 Author Share Posted October 29, 2013 (edited) $eng = 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'); $udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v'); $convertedLanguage = strtr($_POST['box'], array_combine($eng, $udc)); Should work? Edited October 29, 2013 by cjburkey01 Quote Link to comment Share on other sites More sharing options...
cjburkey01 Posted October 29, 2013 Author Share Posted October 29, 2013 $eng = 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'); $udc = array('u', 'd', 'c', 'b', 'o', 'h', 'g', 'f', 'i', 'n', 'm', 'l', 'k', 'j', 'e', 't', 's', 'r', 'q', 'p', 'a', 'z', 'y', 'x', 'w', 'v'); $convertedLanguage = strtr($_POST['box'], array_combine($eng, $udc)); Should work? IT WORKS!!! THANK YOU ALL FOR THE HELP Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.