Jump to content

Recommended Posts

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?

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.

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"

?>

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?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.