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>");
Rich