Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts

    str_replace and Arrays not playing nice.

    PHP Code:
    $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.
    Last edited by pippin418; 03-20-2010 at 11:54 PM.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Run a foreach on the string and encode each character separately.

  • #3
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I haven't really used foreach(). What code would I use to accomplish that?

  • #4
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Anybody?

  • #5
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    $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);

  • Users who have thanked MattF for this post:

    pippin418 (03-21-2010)

  • #6
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Still gives the wrong results. While it has the right number of letters/numbers, it's the wrong combination.

    PHP Code:
    <?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.

  • #7
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Look at the $_REQUEST vars in your code.

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    What about them? I double checked them and they correspond to what they're supposed to.

  • #9
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Actually:
    j_gge Hello3llHello
    After the encoded word (which isn't right) is the response from what I sent.
    $_REQUEST['value2'] == Hello3llHello

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by pippin418 View Post
    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.

  • #11
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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:
    PHP Code:
    $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?
    PHP Code:
    $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.
    Last edited by pippin418; 03-21-2010 at 02:54 AM.

  • #12
    New to the CF scene
    Join Date
    Mar 2010
    Location
    Jakarta, Indonesia
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post
    Try this:
    Code:
    $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.

  • Users who have thanked jnugroho73 for this post:

    pippin418 (03-21-2010)

  • #13
    New Coder
    Join Date
    Dec 2009
    Posts
    40
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jnugroho73 View Post
    Try this:
    Code:
    $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 Code:
    <?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.
    Last edited by pippin418; 03-21-2010 at 08:51 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •