ATTENTION: You are viewing a page formatted for mobile devices; to view the full web page, click HERE.

Other Software > Developer's Corner

[PHP] str_replace - is this even possible?

(1/3) > >>

RedPillow:
Hi again.
I'd like to make simple crypter by myself, this is what I've done so far:


--- Code: PHP ---//This is the key-table for crypter.$cryptkey = array('0' => 286, '1' => 949, '2' => 695, '3' => 348, '4' => 438, '5' => 977,         '6' => 320, '7' => 418, '8' => 970, '9' => 482, '[' => 466, ']' => 770, '.' => 569,        ' ' => 242, '-' => 166, 'A' => 583, 'B' => 575,  'C' => 247,  'D' => 564, 'E' => 192,          'F' => 513,  'G' => 653,  'H' => 605,  'I' => 450,  'J' => 404,  'K' => 831,  'L' => 315,  'M' => 953,           'N' => 439,  'O' => 266,  'P' => 231,  'Q' => 633,  'R' => 881,  'S' => 113,  'T' => 935,  'U' => 559,            'V' => 318,  'W' => 927,  'X' => 906,  'Y' => 463,  'Z' => 184, 'a' => 263,  'b' => 453,  'c' => 208,           'd' => 591,  'e' => 511, 'f' => 343,  'g' => 969,  'h' => 415,  'i' => 420,  'j' => 175,  'k' => 410,           'l' => 295,  'm' => 265, 'n' => 877,  'o' => 200,  'p' => 837,  'q' => 913,  'r' => 515,  's' => 262,           't' => 524,  'u' => 874, 'v' => 525,  'w' => 700,  'x' => 670,  'y' => 918,  'z' => 290, '<' => 331,            '>' => 533, "'" => 425, ',' => 101, ':' => 495, '$' => 114, '\\' => 268, '/' => 721, '=' => 454);//Don't mind about some fails in the table ... solving them later. //This is the string we're going to crypt.$notcrypted1 = "[$curdate] - Rotation was <font color = '$color'><b>$rotation</b></font> degrees, done by <b>$ip</b>, who says: ''<b>$msg</b>''\r\n"; //Splitting the string here. (is this even needed?)$split1 = str_split($notcrypted1); //And this is the problem areaforeach ($split1 as $char) {        foreach ($cryptkey as $char2 => $key2) {                $crypted1 = str_replace($char, $key2, $notcrypted1);                }        } //Im using this PHP.net's example // Provides: Hll Wrld f PHP$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
So basically, it is supposed to replace the characters in the $notcrypted1 with the key-table's corresponding int value.
But the problem is, that I'd like to know if there's any way to tell the program to switch the character it is going trought with the key-table's corresponding int value ... And like without doing 3000 if's.

Since it can replace the vowels with blank in the example - is it possible to switch the characters with the ints in my code?
My brains are jamming with this one ... I know it will not work like this and I even know why it isn't working - I just can't find a way to solve it ...


I wouldn't want to do it like so:


--- Code: PHP ---foreach ($split1 as $char) {        foreach ($cryptkey as $char2 => $key2) {                if ($char == "A") {                        $crypted1 = str_replace($char, "A", $notcrypted1);                }        }

JoTo:
Hi RedPillow,

i think your problem is, that str_replace finds already replaced characters in your string and replace it again and again and again.

This version of your slightly adapted script works for me. Maybe give it a try.


--- Code: PHP ---<?php//This is the key-table for crypter.$cryptkey = array('0' => 286, '1' => 949, '2' => 695, '3' => 348, '4' => 438, '5' => 977,                  '6' => 320, '7' => 418, '8' => 970, '9' => 482, '[' => 466, ']' => 770,                  '.' => 569, ' ' => 242, '-' => 166, 'A' => 583, 'B' => 575, 'C' => 247,                  'D' => 564, 'E' => 192, 'F' => 513, 'G' => 653, 'H' => 605, 'I' => 450,                  'J' => 404, 'K' => 831, 'L' => 315, 'M' => 953, 'N' => 439, 'O' => 266,                  'P' => 231, 'Q' => 633, 'R' => 881, 'S' => 113, 'T' => 935, 'U' => 559,                  'V' => 318, 'W' => 927, 'X' => 906, 'Y' => 463, 'Z' => 184, 'a' => 263,                  'b' => 453, 'c' => 208, 'd' => 591, 'e' => 511, 'f' => 343, 'g' => 969,                  'h' => 415, 'i' => 420, 'j' => 175, 'k' => 410, 'l' => 295, 'm' => 265,                  'n' => 877, 'o' => 200, 'p' => 837, 'q' => 913, 'r' => 515, 's' => 262,                  't' => 524, 'u' => 874, 'v' => 525, 'w' => 700, 'x' => 670, 'y' => 918,                  'z' => 290, '<' => 331, '>' => 533, "'" => 425, ',' => 101, ':' => 495,                  '$' => 114, '\\' => 268, '/' => 721, '=' => 454);//Don't mind about some flaws in table ... solving them later. // Just some dummy values to keep PHP happy :)$curdate = "03/28/2011";$color = "#ffffff";$rotation = "RoundItGoes";$ip = "192.168.0.1";$msg = "Hello world!"; //This is the string we're going to crypt.$notcrypted1 = "[$curdate] - Rotation was <font color = '$color'><b>$rotation</b></font> degrees, done by <b>$ip</b>, who says: ''<b>$msg</b>''\r\n"; //Splitting the string here.$split1 = str_split($notcrypted1); // Replace the characters in the string with their crypted equivalentsforeach($split1 as $key => $value){    if(array_key_exists($value, $cryptkey))        $split1[$key] = $cryptkey[$value];    else    {        // An error occurred, the character is not present in the cryptkey table        // do sth. like outputting an error or inserting a special number or the original         // character or whatever        print "OOPS! An error occurred. Do something, QUICK! :)";    }} // Joining again to get back a single string$crypted1 = implode("", $split1); // Output the crypted resultprint "\nCrypted result is: <".$crypted1.">\n";?>
I get this result string from it:
Crypted result is: <466286348721695970721695286949949770242166242881200524263524420200877242700263262242331343200877524242208200295200515242454242425#343343343343343343425533331453533881200874877591450524653200511262331721453533331721343200877524533242591511969515511511262101242591200877511242453918242331453533949482695569949320970569286569949331721453533101242700415200242262263918262495242425425331453533605511295295200242700200515295591!331721453533425425
>

What looks correct for me, in the way you described your goal, when i check some samples manually.

Hope this helps!

Greetings
JoTo

RedPillow:
Oh my god, it works perfectly! THAAAANKS!
I was worried that nobody wouldn't make any sense of what I want to achieve  ;D

Now, to reverse that back ... Im going to seek for function which switches the key-table's keys with values like so:

OLD:


--- Code: PHP ---$cryptkey = array('0' => 286, '1' => 949, '2' => 695);
NEW:


--- Code: PHP ---$cryptkey = array('286' => 0, '949' => 1, '695' => 2);
Edit: array_flip seems to do the work.

JoTo:
Hi RedPillow again,

YW!

Yes, array_flip exchanges keys with values and vice versa in an array.

You can use the same slightly changed lines to reverse the cryption.

Just split the crypted string with str_split again and use the length parameter with value 3 to get chunks of 3 chars in each array element.
Then use the same foreach construct to search for keys (formerly the values) in cryptkey and replace it by the values (formerly the keys) and you should be there.

Good luck
JoTo

Veign:
Might be easier, and faster to create two arrays: Keys, Values and then use the 'array' find/replace aspect of the str_replace function.

Sample (air-code):
// The setup
$keys = array('a', 'b', 'c');
$values = array('263', '453', '208');

$to_encrypt = 'a b c - abc';
$my_encrypted = str_replace($keys, $values, $to_encrypt);

No loop required.  Much more efficient.

Navigation

[0] Message Index

[#] Next page

Go to full version