FreePgs.com Forum
September 28, 2021, 03:11:21 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Click Here for Toll-Free Service for your business starting at $2.00 per month and 4.5 cents per minute
 
   Home   Help Search Login Register  
Pages: [1] 2   Go Down
  Print  
Author Topic: I must be retarded because I keep screweing up str_replace()  (Read 10592 times)
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« on: June 15, 2006, 01:22:55 AM »

How come this script:
Code:
$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" ? ? ?
Logged

Visite me website at http://www.sixthcrusifix.com
dest
1337 hax0r
Verified Account
Full Member
*

Karma: +0/-0
Offline Offline

Posts: 130


Yar

Blog entries (0)

View Profile
« Reply #1 on: June 15, 2006, 11:51:40 AM »

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
Logged

meh Tongue
FreePgs.com Forum
« Reply #1 on: June 15, 2006, 11:51:40 AM »

 Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #2 on: June 19, 2006, 04:44:13 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?
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #3 on: June 19, 2006, 05:06:59 PM »

What exactly are you trying to accomplish? (You never really said as far as I can see)
Logged
FreePgs.com Forum
« Reply #3 on: June 19, 2006, 05:06:59 PM »

 Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #4 on: June 20, 2006, 06:30:26 AM »

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.
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #5 on: June 20, 2006, 01: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. Smiley
Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #6 on: June 20, 2006, 02:02:26 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. Smiley

But will it work for numbers like 1.2 or numbers like 27? I mean can I use 2, 7 and 27?
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #7 on: June 20, 2006, 03: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 Smiley
Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #8 on: June 20, 2006, 05:37: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 Smiley

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
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #9 on: June 20, 2006, 07:56:19 PM »

Looks good. I'd like to suggest something though Smiley

In the next version you should also encode punctuation (periods, commas, etc.) and have case sensitivity. Looks like you have a good start though Smiley
Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #10 on: June 21, 2006, 12:16:37 AM »

Looks good. I'd like to suggest something though Smiley

In the next version you should also encode punctuation (periods, commas, etc.) and have case sensitivity. Looks like you have a good start though Smiley
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.
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #11 on: June 21, 2006, 12: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)
Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #12 on: June 21, 2006, 01:04:12 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.
Logged

Visite me website at http://www.sixthcrusifix.com
brainiac744
Verified Account
Sr. Member
*

Karma: +10/-0
Offline Offline

Posts: 403

Blog entries (0)

View Profile
« Reply #13 on: June 21, 2006, 09:52:28 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?
Logged
sixthcrusifix
Sage
Verified Account
Sr. Member
*

Karma: +0/-3
Offline Offline

Posts: 445


http://sixthcrusifix.com/home/

Blog entries (0)

View Profile WWW
« Reply #14 on: June 21, 2006, 12:04:53 PM »


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

?
« Last Edit: June 21, 2006, 12:08:03 PM by sixthcrusifix » Logged

Visite me website at http://www.sixthcrusifix.com
Pages: [1] 2   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!