reassembling a string

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Phantom Coder
Posts: 45
Joined: Sun Sep 19, 2010 6:06 am

reassembling a string

Post by Phantom Coder »

Hello
I been learning PHP and i thought WTF let take a string and manipulate it some. I wrote 2 lil snippets that will take a string and pull every odd and and even bits and it works great too if I say so myself.

then i said ok let me see if i can reassemble them back to the original string well the answer is no I cant figure out what to do.

I know i need to take the first bit of the odd place it in a variable, then take the first bit of the even and concatenate it to the to the variable and repeat until the string is reassembled.

so i figuring a for or while loop will be needed.

my string is 1234567890 and the variable $oddstring will hold 13579 while the variable $evenstring will hold 24680 i trying to reassemble them back into a variable called $originalstring like this 1234567890

my code is below: I really could use some help on this one so PLEASE head me in the right direction or sample code on how to do what im trying to do. I searched the web and could not find anything on it.

Code: Select all


$str = "1234567890";

/** grabs the even bits from $str only */
$evenstring = '';
$str_length =  strlen($str);
    for($i=0; $i< $str_length; $i++)
    {
        if($i%2!=0) $evenstring .= $str{$i};
    }

/** grabs the odd bits from $str only */ 
$oddstring = '';
$str_length =  strlen($str);
    for($i=0; $i< $str_length; $i++)
    {
        if($i%2==0) $oddstring .= $str{$i};
    }

/** grabs the bits from $oddstring and $evenstring and reassemble them in $originalstring */
$originalstring = '';
$str_length =  strlen($oddstring);
$str2_length = strlen($evenstring);

   //i got this far but don't know if im looking in the right direction 	
   



echo("The even bits are: " . $evenstring . "<br>");
echo("The odd bits are: " . $oddstring . "<br>");
echo("The completed string is: " . $originalstring . "<br>");
Thanks
Rich
User avatar
Chris
Posts: 1580
Joined: Wed Sep 30, 2009 7:22 pm

Re: reassembling a string

Post by Chris »

Code: Select all

$originalString = '';
for(  $i=0; $i<= round( strlen($evenstring) + strlen($oddstring) / 2); $i++; )
{
      $originalString .= $evenstring[$i] . $oddstring[$i];
} 
Fighting for peace is declaring war on war. If you want peace be peaceful.
User avatar
Phantom Coder
Posts: 45
Joined: Sun Sep 19, 2010 6:06 am

Re: reassembling a string

Post by Phantom Coder »

thanks i figured it out for some reason im not getting notified about posts i post here.
this is how i ended up doing it.

Code: Select all

    /** THIS METHOD ASSEMBLES THE SITEKEY */
    function assembleSitekeyString()
    {

       /** grabs the bits from $oddstring and $evenstring 
       and reassemble them in $originalstring */
       $originalstring = '';
       $str_length =  strlen($this->oddstring);
       
           for($i=0; $i< $str_length; $i++){
            
            $originalstring .= $this->oddstring{$i} . $this->evenstring{$i};
           }
           
           echo("The reassembled string is: " . $originalstring . "<br><br>");
           return $originalstring;
    }

Im working on a password hashing system that will store the sitekey, salt, and secret key insider the password hash or encapsulated hash, and dissamble the hash so that the sitekey, salt, secretkey can be reassembled to create the password salt so it can rehash the user imput to check against the salted password hash to allow or denies access to the website. i figure this way you can have a completely different set of random keys for each user and not have to worry about the keys getting stolen since when it put together it really looks like a whirlpool or sha512 hash.
Post Reply

Return to “Advanced Help and Support”