need help with str_replace and array

Discussion in 'PHP' started by DjZoC, Nov 25, 2011.

  1. #1
    Hello there, im working to some code but i get stuck hope someone can help me with this

     function id( $id ) {
      $1 = array(
       "1",   "t",   "r"
      );
    
      if (preg_match("/1/i", $id)) {
       $id = str_replace('1', $1[rand(0,3)], $id);
      }
    
      return $id;
    
     }
    PHP:
    if i put like this

    echo id( '1221133' );

    will show me " r22rr33 " how i can make to get random value for every 1 example to be " r22t133 "
     
    DjZoC, Nov 25, 2011 IP
  2. windrock

    windrock Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You can use for ()

    
    $newstr = '';
    for ($i = 0; $i < strlen($str); $i++)
    {if ($str[$i] == '1') {$newstr.= rand(0,3);} else {$newstr .= $str[$i];}
    }
    echo $newstr;
    
    PHP:
     
    windrock, Nov 26, 2011 IP
  3. DjZoC

    DjZoC Member

    Messages:
    167
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    hello there, i try this command but its not working, what im try to do its my own encrypt code
     
    DjZoC, Nov 26, 2011 IP
  4. thinglet

    thinglet Peon

    Messages:
    29
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    One issue with your existing code is that rand might be calling a non-existent element. It should be rand(0,2) since there are only 3 items total in the $1 array.

    Can you tell me how you plan on reversing the encryption, since there's a possibility of 1 being replaced by 1?

    Thanks
     
    thinglet, Nov 26, 2011 IP
  5. DjZoC

    DjZoC Member

    Messages:
    167
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    The script can show how lol :) 1 will not be all time 1 can be t or r hard to find the real code ... anyway what i put there on the script its just example i just need the command to can get every 1 with difrent value 1 or t or r exampe 1211223 = r2t1223 or t2tr223 or etc ...
     
    DjZoC, Nov 26, 2011 IP
  6. Jaxo

    Jaxo Peon

    Messages:
    33
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #6
    You can't have variables that are just a number, or starting with a number.

    $1 will not work.

    $asdf is better.
     
    Jaxo, Dec 14, 2011 IP