FreePgs.com Forum

FreePgs Related => Support Requests => Topic started by: sixthcrusifix on June 15, 2006, 05:22:55 AM

Title: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 15, 2006, 05:22:55 AM
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" ? ? ?
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 19, 2006, 08:44:13 PM
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?
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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)
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 20, 2006, 10:30:26 AM
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.
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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. :)
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 20, 2006, 06:02:26 PM
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?
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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 :)
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 20, 2006, 09:37:54 PM
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
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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 :)
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 21, 2006, 04:16:37 AM
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.
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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)
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 21, 2006, 05:04:12 AM
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.
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: 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?
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 21, 2006, 04:04:53 PM
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

?
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: brainiac744 on June 21, 2006, 06:19:33 PM
You COULD do that, or you could go ahead and leave it as one string, and then split it up in php (adding in seperators). You're right about it possibly incorrectly parsing though...

What I was suggesting would be to split the string up first by using something like

str_split();

http://us3.php.net/manual/en/function.str-split.php

Then you can match the values in the array without having to worry about things getting crossed.
Title: Re: I must be retarded because I keep screweing up str_replace()
Post by: sixthcrusifix on June 21, 2006, 09:00:00 PM
Quote from: brainiac744 on June 21, 2006, 06:19:33 PM
You COULD do that, or you could go ahead and leave it as one string, and then split it up in php (adding in seperators). You're right about it possibly incorrectly parsing though...

What I was suggesting would be to split the string up first by using something like

str_split();

http://us3.php.net/manual/en/function.str-split.php

Then you can match the values in the array without having to worry about things getting crossed.

oh I have been using str_split but not in that way, hmmm. .

I have some useless code and I have some valid stuff and I think i need to clean it up first.

I changed my working version so that the seperators are .'s on the left and numbers on the right. So that 32, 34.55, 67, 89.1 would look possibly like this: .324.34.554.674.89.14
and it pases correctly, giving incorrect decoding only when particular seeds are used (Specifically hello and qwerty, loll).

But that's just a hokey guise. I could even make the number seperator generated through the seed to, but I much like your idea better, I want it to be extremly difficult to even be able to see the seperations in letters.

I was also thinking of not using str_replace and instead simply splitting the original message into an array, and then somehow combinign it into a number array and creating a band new array with each wordjavascript:void(0);
Insert Quote coded and then imploding it into the new coded message, I just haven't figured out how I'd get it back. (I just re-read what you wrote and realised that this is what you suggested LOLL I'll look into it)

Thanks :D
___________________________________________________________

I decided to try to completely rewrite the whole thing from scratch and I messed up somewhere. The values change drastically depending on teh seed but . . . all of the letters have the exact same value . . .? ? ? I've actually included my code this time just don't laugh at the simplicity of it, I intend on cleaning it up and making a crypt class.


<?php
//the variabel message is really the seed

function seed($MSG,$message,$what){
$MSG strtolower($MSG);
//$message = strtolower($message);
  
$new md5($message);
  
$new.= strrev($new);
  
$new str_split($new);
  
$var1 strlen($message);
  
$var2 $var1-($var1*.90);
  
$var2 floor($var2);
  
$S = array(".",":");
  
$vowels = array("a","e","i","o","u","A","E","I","O","U");
$stringarray str_split($MSG);
$str_size count($stringarray);
$startnums = array();
$I 0;$II 0;
$seed_size count($new);

$chartonum = array
(
"q"=>'10',"w"=>'35',
"e"=>'11',"r"=>'34',
"t"=>'12',"y"=>'33',
"u"=>'13',"i"=>'32',
"o"=>'14',"p"=>'15',
"a"=>'16',"s"=>'17',
"d"=>'18',"f"=>'19',
"g"=>'20',"h"=>'21',
"j"=>'22',"k"=>'23',
"l"=>'24',"z"=>'25',
"x"=>'26',"c"=>'27',
"v"=>'28',"b"=>'29',
"n"=>'30',"m"=>'31',
);
$multiplier 0;
$ii 0;
while(
$ii<$seed_size){
     if(
is_numeric($new[$ii])){
 $multiplier+=$new[$ii];

     }
 
$ii++;
}

/////////////////////////
 
while($I<$str_size){
   while(
$II<$seed_size){
     if(
is_numeric($new[$II]))
     { 
     
$value = (round(($new[$II]*(.1*$multiplier)),1)); 
     }else
     {
     
$value $chartonum[$new[$II]];
     }
 $II++;
   } 
 
$startnums[$I]=$value;
 
$I++;
 } 
 
$search $stringarray;
$replace $startnums;
$subject $stringarray;
//
 
if($what==1){
 
$new_msg str_replace($search,$replace,$subject);
 }elseif(
$what==2){
 
$new_msg str_replace($replace,$search,$subject); 
 }
 
$new_msg implode('',$new_msg);

return 
$new_msg
}