Welcome Guest.   Make a donation to an author on the site December 18, 2014, 02:35:26 AM  *

Please login or register.
Or did you miss your validation email?


Login with username and password (forgot your password?)
Why not become a lifetime supporting member of the site with a one-time donation of any amount? Your donation entitles you to a ton of additional benefits, including access to exclusive discounts and downloads, the ability to enter monthly free software drawings, and a single non-expiring license key for all of our programs.


You must sign up here before you can post and access some areas of the site. Registration is totally free and confidential.
 
Read the full one-year retrospective report on DonationCoder.com.
   
   Forum Home   Thread Marks Chat! Downloads Search Login Register  
Pages: [1]   Go Down
  Reply  |  New Topic  |  Print  
Author Topic: [PHP] str_replace - is this even possible?  (Read 3272 times)
RedPillow
Member
**
Posts: 140


Pillows.

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« on: March 28, 2011, 05:15:58 AM »

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

Formatted for PHP with the GeSHI Syntax Highlighter [copy or print]
  1. //This is the key-table for crypter.
  2. $cryptkey = array('0' => 286, '1' => 949, '2' => 695, '3' => 348, '4' => 438, '5' => 977,
  3. '6' => 320, '7' => 418, '8' => 970, '9' => 482, '[' => 466, ']' => 770, '.' => 569,
  4. ' ' => 242, '-' => 166, 'A' => 583, 'B' => 575,  'C' => 247,  'D' => 564, 'E' => 192,
  5. 'F' => 513,  'G' => 653,  'H' => 605,  'I' => 450,  'J' => 404,  'K' => 831,  'L' => 315,  'M' => 953,
  6.  'N' => 439,  'O' => 266,  'P' => 231,  'Q' => 633,  'R' => 881,  'S' => 113,  'T' => 935,  'U' => 559,
  7.   'V' => 318,  'W' => 927,  'X' => 906,  'Y' => 463,  'Z' => 184, 'a' => 263,  'b' => 453,  'c' => 208,
  8.   'd' => 591,  'e' => 511, 'f' => 343,  'g' => 969,  'h' => 415,  'i' => 420,  'j' => 175,  'k' => 410,
  9.   'l' => 295,  'm' => 265, 'n' => 877,  'o' => 200,  'p' => 837,  'q' => 913,  'r' => 515,  's' => 262,
  10.   't' => 524,  'u' => 874, 'v' => 525,  'w' => 700,  'x' => 670,  'y' => 918,  'z' => 290, '<' => 331,
  11.   '>' => 533, "'" => 425, ',' => 101, ':' => 495, '$' => 114, '\\' => 268, '/' => 721, '=' => 454);
  12. //Don't mind about some fails in the table ... solving them later.
  13.  
  14. //This is the string we're going to crypt.
  15. $notcrypted1 = "[$curdate] - Rotation was <font color = '$color'><b>$rotation</b></font> degrees, done by <b>$ip</b>, who says: ''<b>$msg</b>''\r\n";
  16.  
  17. //Splitting the string here. (is this even needed?)
  18. $split1 = str_split($notcrypted1);
  19.  
  20. //And this is the problem area
  21. foreach ($split1 as $char) {
  22. foreach ($cryptkey as $char2 => $key2) {
  23. $crypted1 = str_replace($char, $key2, $notcrypted1);
  24. }
  25. }
  26.  
  27. //Im using this PHP.net's example
  28.  
  29. // Provides: Hll Wrld f PHP
  30. $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
  31. $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:

Formatted for PHP with the GeSHI Syntax Highlighter [copy or print]
  1. foreach ($split1 as $char) {
  2. foreach ($cryptkey as $char2 => $key2) {
  3. if ($char == "A") {
  4. $crypted1 = str_replace($char, "A", $notcrypted1);
  5. }
  6. }

« Last Edit: March 28, 2011, 05:28:31 AM by RedPillow » Logged
JoTo
Super Honorary
Charter Member
***
Posts: 238



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #1 on: March 28, 2011, 06:10:51 AM »

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.

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

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
« Last Edit: March 28, 2011, 06:18:52 AM by JoTo » Logged
RedPillow
Member
**
Posts: 140


Pillows.

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #2 on: March 28, 2011, 06:46:32 AM »

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

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

OLD:

Formatted for PHP with the GeSHI Syntax Highlighter [copy or print]
  1. $cryptkey = array('0' => 286, '1' => 949, '2' => 695);

NEW:

Formatted for PHP with the GeSHI Syntax Highlighter [copy or print]
  1. $cryptkey = array('286' => 0, '949' => 1, '695' => 2);

Edit: array_flip seems to do the work.
« Last Edit: March 28, 2011, 06:51:05 AM by RedPillow » Logged
JoTo
Super Honorary
Charter Member
***
Posts: 238



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #3 on: March 28, 2011, 07:14:53 AM »

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
Logged
Veign
Charter Honorary Member
***
Posts: 993



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #4 on: March 28, 2011, 09:02:42 AM »

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

RedPillow
Member
**
Posts: 140


Pillows.

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #5 on: March 28, 2011, 10:31:57 AM »

Since I got your attention already, I'll ask if anyone knows, if it is possible in anyway for random <|p> and <|/p> -tags to get insterted into my code.
ALSO, str_replace wont find em either.

This is the output, don't mind about the contents  embarassed


Formatted for Text with the GeSHI Syntax Highlighter [copy or print]
  1. <p>[18.26.23] - Rotation was <font color = 'orange'><b>225</b></font> degrees, done by <b>127.0.0.1</b>, who says: '<b>HIGHWAY|||</p><p>|||-CHESS BOARD|||</p><p>|||</b>'


I've highlighted the tags with '|||'s, which cause problem and I assure you, that I've gone trought EVERY FILE RELATED trying to find where those tags pop-up.
I've tried using find in text-editor, I've tried to strip and trim them and no I don't know what to do anymore...

This is just my foolish theory but ... might it be possible, since 'HIGHWAY' and 'CHESSBOARD' are obtained from text-file,
that they somehow contain invisible tags or something after them? Like HIGHWAY<|/p><|p> ?

« Last Edit: March 28, 2011, 10:41:31 AM by RedPillow » Logged
Veign
Charter Honorary Member
***
Posts: 993



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #6 on: March 28, 2011, 11:00:28 AM »

No such thing as invisible tags in a text file.  Your code has to be adding them or the text files have to already contain them.  Text files have 0 knowledge of HTML.
Logged

RedPillow
Member
**
Posts: 140


Pillows.

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #7 on: March 28, 2011, 11:03:24 AM »

Still weird tho ... I can print ie. CHESSBOARD as much as I want and it always puts <|/p><|p> after the word.  huh
« Last Edit: March 28, 2011, 11:08:21 AM by RedPillow » Logged
Veign
Charter Honorary Member
***
Posts: 993



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #8 on: March 28, 2011, 11:22:13 AM »

If you're taking raw text from a text file, processing it and spitting out some cleaned/washed version then your code is adding it or it exists in plain site.  No other options.
Logged

JoTo
Super Honorary
Charter Member
***
Posts: 238



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #9 on: March 28, 2011, 12:05:43 PM »

Hi Veign,

i was thinking about the array functionality of str_replace as well before i posted my solution of course. Problem is, that the replacements contain characters that will be replaced again (are in the search array).

E.g. if an "E" gets replaced with "012" and a "1" with "345" you end up with "03452" when using this approach. I've tested it first before i came back to my posted solution. As the characters in his string are random, you cant find an order of the search array that prevents this "reentrance".

Seems str_replace is not made for that you replace sth. with sth. that is in the search array. So this approach is not more effective but just won't work at all. smiley Therefore this workaround.

For the other point i must agree with Veign. No black magic or voodoo here. Either the tags are in your source text already or your code adds them.

Greetings
JoTo
« Last Edit: March 28, 2011, 12:09:40 PM by JoTo » Logged
Veign
Charter Honorary Member
***
Posts: 993



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #10 on: March 28, 2011, 01:26:25 PM »

You are correct JoTo.  Not sure why I thought it would work.  Makes sense that it wouldn't.  Its loops through the already-replaced results looking to replace more items.  What they need is flag that would prevent an item from being replaced twice with the order of replace dictated by the order of the arrays passed into the function.
Logged

JoTo
Super Honorary
Charter Member
***
Posts: 238



see users location on a map View Profile Give some DonationCredits to this forum member
« Reply #11 on: March 28, 2011, 11:51:45 PM »

Hi Veign,

glad that you proofed it. I just thought i am too dumb again to use the str_replace function correctly and gave out a bad advise.

Happy replacing all smiley
JoTo
Logged
RedPillow
Member
**
Posts: 140


Pillows.

see users location on a map View Profile Read user's biography. Give some DonationCredits to this forum member
« Reply #12 on: March 29, 2011, 10:35:10 AM »

The <|/p> and <|p> tags somehow come from joining these two together:

$Firstpart[$rand] . $SecondPart[$rand];
Logged
Veign
Charter Honorary Member
***
Posts: 993



see users location on a map View Profile WWW Give some DonationCredits to this forum member
« Reply #13 on: March 29, 2011, 11:29:59 AM »

Nope.  Didn't come from joining.  Code constructs have no clue what HTML is.  The variable had to have already contained the information.
Logged

Pages: [1]   Go Up
  Reply  |  New Topic  |  Print  
 
Jump to:  
   Forum Home   Thread Marks Chat! Downloads Search Login Register  

DonationCoder.com | About Us
DonationCoder.com Forum | Powered by SMF
[ Page time: 0.419s | Server load: 0.04 ]