...

View Full Version : str_replace and Arrays not playing nice.



pippin418
03-20-2010, 10:42 PM
$str = $_REQUEST['value2'];
$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$eng = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");
$mes = str_replace($alp,$eng,$str);
echo $mes . "<reset>";

In there, I had ,"\\", for a backslash, but I took that out, since I didn't think that would work. This is supposed to encode strings and make them secret. It isn't working. "hi" (no quotes) returns "aChV" (no quotes).

When the string hi should return aV, it gives me aChV. The "<reset>" is for the service that is passing POST variables through the script, disregard it.

I can't figure out why. Can you?

EDIT:
I've verified that $_REQUEST['value2'] does equal hi in this example. So it's not that the string is not hi.

MattF
03-20-2010, 10:57 PM
Run a foreach on the string and encode each character separately.

pippin418
03-20-2010, 11:04 PM
I haven't really used foreach(). What code would I use to accomplish that?

pippin418
03-20-2010, 11:56 PM
Anybody?

MattF
03-21-2010, 12:34 AM
$output = '';
$string = str_split($_REQUEST['value2']);
$find = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$replace = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");

foreach ($string as $char)
{
$output .= str_replace($find, $replace, $char);
}
print($output);

pippin418
03-21-2010, 12:56 AM
Still gives the wrong results. While it has the right number of letters/numbers, it's the wrong combination.


<?php
if (strtolower($_REQUEST['msg']) == "cancel") {
echo "<reset>";
die("Canceled.");
}
switch ($_REQUEST['step']) {
case 1:
echo "Menu:\r\n
1. Encode string\r\n
2. Decode string";
break;
case 2:
echo "Please enter the message.";
break;
case 3:
if ($_REQUEST['value1'] = 1) {
$output = '';
$string = str_split($_REQUEST['value2']);
$find = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$replace = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");

foreach ($string as $char)
{
$output .= str_replace($find, $replace, $char);
}
print($output);
echo "<reset>";
}
if ($_REQUEST['value1'] = 2) {
$output = '';
$string = str_split($_REQUEST['value2']);
$replace = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$find = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");

foreach ($string as $char)
{
$output .= str_replace($find, $replace, $char);
}
print($output);
echo "<reset>";
}
break;
}
?>

That's the entire code. I reversed the arrays names for decode... No luck.

MattF
03-21-2010, 01:06 AM
Look at the $_REQUEST vars in your code.

pippin418
03-21-2010, 01:08 AM
What about them? I double checked them and they correspond to what they're supposed to.

pippin418
03-21-2010, 01:12 AM
Actually:
j_gge Hello3llHello
After the encoded word (which isn't right) is the response from what I sent.
$_REQUEST['value2'] == Hello3llHello

MattF
03-21-2010, 01:25 AM
What about them? I double checked them and they correspond to what they're supposed to.

As in look at them and print them out, to check what values are being supplied to the encode/decode loops. If the output is incorrect, you're supplying something incorrectly to the respective parts of the script.

pippin418
03-21-2010, 01:35 AM
The copy of the script on my laptop server gave me interesting results.
I commented out all of the letters after the lowercase ones in the array.

<?php
$str = $_POST['a'];
$output = '';
$string = str_split($str);
$find = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"); //,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$replace = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I");//,"1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");

foreach ($string as $char)
{
$output .= str_replace($find, $replace, $char);
}
print_r($output);
?>

I noticed that processing abcdefghijklmnopqrstuvwxyz (a4~Y3}gaV:&g7@eTTA0NIZ"'xI) through the encoder takes the a in the beginning as a g. So instead of giving me an h which is what an a is, it gave me an a, which is what g is. I checked $str (which is exclusive to the home version) and it is equilvalent to what I put through the encoder. A couple other letters are like that. I also noticed:


$find = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"); //,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$replace = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I");//,"1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");

That both g and l are in the same spot, as in g corresponds to l and l corresponds to g. Look at the $find and $replace buddies (the ones either above or below them). There's a few other ones like that.

Do you see?


$find = array("g", "l");
$replace = array("l", "g");


EDIT:
Making the $replace array 1234567890abcdefghijklmnop for all of the lowercase letters, putting in the lowercase alphabet worked. The problem was stated above with the "buddies".
EDIT:
But problem not solved. This is so confusing. I entered completely random (none repeating or having "buddies") values back into the array, and I still have the same problem.

jnugroho73
03-21-2010, 02:34 AM
Try this:


$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$eng = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");
$nalp=array();
WHILE(list($k,$v)=each($alp)){
$nalp[$v]=$k;
}
$mes='';
foreach($ar as $ch){
$mes.=$eng[$nalp[$ch]];
}
echo $mes . "<reset>";


Above I tried to reverse the $alp value and $alp key so of each letter to be translated I can get the key from array $eng, thus easily obtain the correct value of $eng.

pippin418
03-21-2010, 07:46 AM
Try this:


$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$eng = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");
$nalp=array();
WHILE(list($k,$v)=each($alp)){
$nalp[$v]=$k;
}
$mes='';
foreach($ar as $ch){
$mes.=$eng[$nalp[$ch]];
}
echo $mes . "<reset>";


Above I tried to reverse the $alp value and $alp key so of each letter to be translated I can get the key from array $eng, thus easily obtain the correct value of $eng.

That script is not in any way valid.
1. $ar is non existent.
2. $ar is not an array so foreach is wrong.
3. The string to convert isn't in there.


BUT:
The working script is (drumroll):

<?php
$str = $_POST['a'];
$alp = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",".","/",",","!","@","#","$","%","^","&","*","(",")","_","-","+","=","{","[","]","}",":".";"."\"","'","?","<",">","~","`","1","2","3","4","5","6","7","8","9","0","|");
$eng = array("h","4","~","Y","3","}","l","a","V",":","&","g","7","@","e","q","T","A","0","N","z","Z","\"","'","x","I","1","+","i","R","d","p","2","^","D",",",";",".","c","5","[",")","$","`","!","#","|","C","J","k","6","/","W","8","%","*","(","E","b","f","j","m","K","S","?","H","Q","L","B","G","P","M","U","X","O","F","9","]", "{","<","=",">","_","-","u","r","w","s","t","v","y","o","n");
$nalp=array();
WHILE(list($k,$v)=each($alp)){
$nalp[$v]=$k;
}
$mes='';
$str2 = str_split($str);
foreach($str2 as $ch) {
$mes.=$eng[$nalp[$ch]];
}
echo $mes . "<reset>";
?>

Thanks for all your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum