News:

The "Support Requests" forum is now viewable by guests.

Main Menu

I must be retarded because I keep screweing up str_replace()

Started by sixthcrusifix, June 15, 2006, 05:22:55 AM

Previous topic - Next topic

sixthcrusifix

How come this script:

$string = "abc bca";
$letters = array('a','b','c');
$numbers = array('b','c','a');

$str = str_replace($letters,$numbers,$string);

echo($str);


Returns the string "aaa aaa" ? ? ?
Visite me website at http://www.sixthcrusifix.com

dest

I'll go through this as PHP would

1) replace all a with b
result: bbc bcb

2) replace all b with c
result: ccc ccc

3) replace all c with a
result: aaa aaa
meh :P

sixthcrusifix

Quote from: dest on June 15, 2006, 03:51:40 PM
I'll go through this as PHP would

1) replace all a with b
result: bbc bcb

2) replace all b with c
result: ccc ccc

3) replace all c with a
result: aaa aaa


AAAAAHHHH I SEE!!

So how can I accomplish what I'm trying to accomplish?
Visite me website at http://www.sixthcrusifix.com

brainiac744

What exactly are you trying to accomplish? (You never really said as far as I can see)

sixthcrusifix

Quote from: brainiac744 on June 19, 2006, 09:06:59 PM
What exactly are you trying to accomplish? (You never really said as far as I can see)


OOh A long time ago I was tryng to make an Al Bhed translater (teh FFX Language) and I was tryign to replace the letters.

Now I'm doing something slightly different where I'm trying to replace letters with random numbers.

There is a custom number generator that generates a number for each letter in an array. There are also a number of seeds that determine what numbers it will create. The numbers replace the lettersand the new message contains the number message along with a seed ID so that the message can be interpreted again and return the originel coded message. The seeds are infinite because they're based on basic text. so

if you create the message "Hello world" with the seed "abc" it can be interpreted with the abc seed, but the seeds are not predefined you just type in a name for your seed and it is used.

I thoguht it was just somethign cool to experiement with.
Visite me website at http://www.sixthcrusifix.com

brainiac744

If you're replacing letters with numbers (as long as a number isn't used more than once, wouldn't work otherwise), then str_replace should work correctly with an array, in fact I've used str_replace to translate a phrase into numbers before so I know it works. :)

sixthcrusifix

Quote from: brainiac744 on June 20, 2006, 05:10:36 PM
If you're replacing letters with numbers (as long as a number isn't used more than once, wouldn't work otherwise), then str_replace should work correctly with an array, in fact I've used str_replace to translate a phrase into numbers before so I know it works. :)

But will it work for numbers like 1.2 or numbers like 27? I mean can I use 2, 7 and 27?
Visite me website at http://www.sixthcrusifix.com

brainiac744

You can use different numbers like 2, 7 and 27. I don't think there would be a problem with decimals either. Just keep in mind that if you have a 2 and a 7 back to back the script won't know whether to translate it back into the letters for 2 and 7 or the letter for 27 :)

sixthcrusifix

Quote from: brainiac744 on June 20, 2006, 07:35:54 PM
You can use different numbers like 2, 7 and 27. I don't think there would be a problem with decimals either. Just keep in mind that if you have a 2 and a 7 back to back the script won't know whether to translate it back into the letters for 2 and 7 or the letter for 27 :)

It ended up screwing up, exactly how you said it would. The solution was to sorround the numbers with seperators.

This is what I have done so far:http://crystalchasm.net/testpage2.php
Visite me website at http://www.sixthcrusifix.com

brainiac744

Looks good. I'd like to suggest something though :)

In the next version you should also encode punctuation (periods, commas, etc.) and have case sensitivity. Looks like you have a good start though :)

sixthcrusifix

Quote from: brainiac744 on June 20, 2006, 11:56:19 PM
Looks good. I'd like to suggest something though :)

In the next version you should also encode punctuation (periods, commas, etc.) and have case sensitivity. Looks like you have a good start though :)
Yeah I wanna include those things. But first I have to fix my little problem.

The numbers are generated by a 10% of teh string length rounded up plus or minus the number of the letter in the array.

The problem is that because I did it this way the letters have a pretty predictable pattern. If they were arranged in ABC fashion, A might be 3.3 and B might be 3.44445 but 12.33 is usually L and teh last letter is always exactly 26 . . . it's weird.
Visite me website at http://www.sixthcrusifix.com

brainiac744

I think that what I'd do is make sure that each letter is a two-digit number, and then lose the letter seperators. That makes it a bit more cryptic and I think that should also take care of the multiples problem (especially if you get the script to magically break it up into two-digit items in an array)

sixthcrusifix

Quote from: brainiac744 on June 21, 2006, 04:54:32 AM
I think that what I'd do is make sure that each letter is a two-digit number, and then lose the letter seperators. That makes it a bit more cryptic and I think that should also take care of the multiples problem (especially if you get the script to magically break it up into two-digit items in an array)

oh I never thought of making them two digit numbers, that could totaly end the need for the seperators. my only problem would be this:

let's say the string turns into 12331121 (12, 33, 11, 21)
What keeps the script from turning that into 1, 23, 31 etc.
Visite me website at http://www.sixthcrusifix.com

brainiac744

Quote from: sixthcrusifix on June 21, 2006, 05:04:12 AM

oh I never thought of making them two digit numbers, that could totaly end the need for the seperators. my only problem would be this:

let's say the string turns into 12331121 (12, 33, 11, 21)
What keeps the script from turning that into 1, 23, 31 etc.

1 isn't a two digit number, so that's not really a problem, or am I missing something?

sixthcrusifix

#14
Quote from: brainiac744 on June 21, 2006, 01:52:28 PM
Quote from: sixthcrusifix on June 21, 2006, 05:04:12 AM

oh I never thought of making them two digit numbers, that could totaly end the need for the seperators. my only problem would be this:

let's say the string turns into 12331121 (12, 33, 11, 21)
What keeps the script from turning that into 1, 23, 31 etc.

1 isn't a two digit number, so that's not really a problem, or am I missing something?

Well if PHP reads it like you said, PHP might look for 23 before it looks for 12. So 1 will just sit there unparsed and 12 and 33 with become 23 and then a chain reaction of single unparsed numbers and incorrect numbers. . . right?

What if there is only one seperator on the left side and spaces and punctuation are also included?

a string might look like this:
23.45.66.66.78.10.76.78.43.66.55.
which might read:
hello world

?
Visite me website at http://www.sixthcrusifix.com