smartboyathome Posted October 19, 2009 Share Posted October 19, 2009 I am trying to switch letters around in a message for an application on my site (see here). The problem is that I can't figure out a way to get it to work. What I am trying to do is for example switch a with n, and n with a, so that a message like "no one at all" would become "ao oae nt nll". I tried using str_replace plus a couple arrays, but it doesn't work. It instead creates something like "no one nt nll". Anyone have any idea how would I get this to work? Quote Link to comment Share on other sites More sharing options...
sastro Posted October 19, 2009 Share Posted October 19, 2009 You should put the character in array the shuffle Quote Link to comment Share on other sites More sharing options...
TexasMd91 Posted October 19, 2009 Share Posted October 19, 2009 The only way I can think of doing it (and I usually do it the worst way possible, so maybe some one else's reply will be better) is to have a buffer-string and when you want to switch N with A and A with N, you can get the location (the index) of where the A's used to be from the buffer. Quote Link to comment Share on other sites More sharing options...
Psycho Posted October 19, 2009 Share Posted October 19, 2009 I thinkth eproblem is that you are trying to replace the same letters with the other values that you are replacing. So, there is some sort of recursive error that is going on. You replace 'a' with 'n', but you are also replacing 'n' with 'a'. I tried str_replace() and obtained different results - only the first two replacements appeared to be made. But, i suspect that the other replacemetns were made, but they were made back again. Input: "no one at all" Output: "ao oae at all" My suggestion would be to do in intermediary replacement. First, change the letters to something else so you don't have the problem of replacing something with another value that is also being replaced. <?php $text = '"no one at all"'; echo "Input: $text<br>"; $search = array('a', 'n'); $replace1 = array('[a]', '[n]'); $replace2 = array('n', 'a'); $text = str_replace($search, $replace1, $text); $text = str_replace($replace1, $replace2, $text); echo "Output: $text"; //Input: "no one at all" // //Output: "ao oae nt nll" ?> Quote Link to comment Share on other sites More sharing options...
smartboyathome Posted October 19, 2009 Author Share Posted October 19, 2009 Thanks guys, I finally got it to start working. I converted the message into an array, with one character per element, then had str_replace focus on only one element at a time. Finally, I imploded the thing to make it a string again. Here is the code: $message = $_POST['message']; $a = 0; while(isset($message{$a})) { $newMessage[$a] = $message{$a}; $a++; } $replace = 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'); $with = array('b', 'w', 'v', 'a', 'k', 'h', 'c', 'u', 'i', 's', 'j', 'z', 'o', 'y', 'm', 'n', 'd', 'p', 'r', 'q', 'g', 'x', 't', 'f', 'e', 'l'); $then = array("<img src=\"a.png\">","<img src=\"b.png\">","<img src=\"c.png\">","<img src=\"d.png\">","<img src=\"e.png\">","<img src=\"f.png\">","<img src=\"g.png\">","<img src=\"h.png\">","<img src=\"i.png\">","<img src=\"j.png\">","<img src=\"k.png\">","<img src=\"l.png\">","<img src=\"m.png\">","<img src=\"n.png\">","<img src=\"o.png\">","<img src=\"q.png\">","<img src=\"r.png\">","<img src=\"s.png\">","<img src=\"t.png\">","<img src=\"u.png\">","<img src=\"v.png\">","<img src=\"w.png\">","<img src=\"x.png\">","<img src=\"y.png\">","<img src=\"z.png\">"); $a = 0; while(isset($newMessage[$a])) { $almostScrambled[$a] = str_ireplace($replace,$then,$newMessage[$a]); $a++; } $scrambled = implode($almostScrambled); $scrambled = str_replace("\\'","'",$scrambled); echo $scrambled; I am having a new problem, though. I try to replace each element with the appropriate image representation of the character. The problem is that it just outputs a whole bunch of the end (.png"> I think, I can't get it to do that now). Now the script is just exhausting all the allocated memory (100mbs) and won't work. Why is it doing this? 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.