On 28/02/2008, Robert Cummings <[EMAIL PROTECTED]> wrote:
>
>  On Thu, 2008-02-28 at 12:39 +0000, Nathan Rixham wrote:
>  > Aschwin Wesselius wrote:
>  > > Stut wrote:
>  > >> Just because it works doesn't mean it's right.
>  > >>
>  > >> -Stut
>  > >>
>  > >
>  > >
>  > > What I meant was that I tested the script and it worked, so I didn't
>  > > spot the flaw (wich is obvious and you were right).
>  > >
>  > > No big deal.
>  > >
>  >
>  > should it not use curlies?
>  >
>  > $tmp = '';
>  > $str = 'abcdef';
>  > for ($i = strlen($str)-1; $i >= 0; $i--) {
>  >   $tmp.= $str{$i};
>  > }
>  > echo $tmp;
>  >
>  >
>  > here's the "overkill" way to do it:
>  >
>  > $str = 'abcdef';
>  > $rev = implode(array_flip(array_reverse(array_flip(str_split($str)))));
>  > echo $rev;
>  >
>  > *sniggers*
>
>
> There's always a tradeoff between speed and memory. Here's the low
>  memory version:
>
>  <?php
>
>  $str = '1234567';
>  str_reverse_in_place( $str );
>  echo 'Reversed: '.$str."\n";
>
>  function str_reverse_in_place( &$str )
>  {
>     $a = 0;
>     $z = strlen( $str ) - 1;
>
>     while( $a < $z )
>     {
>         $t = $str[$a];
>         $str[$a] = $str[$z];
>         $str[$z] = $t;
>
>         ++$a;
>         --$z;
>     }
>  }
>
>  ?>


every byte counts :-)

function str_reverse_in_place( &$str )
{
   $a = -1;
   $z = strlen($str);

   while( ++$a < --$z )
   {
       $str[$a] = $str[$a] ^ $str[$z];
       $str[$z] = $str[$a] ^ $str[$z];
       $str[$a] = $str[$a] ^ $str[$z];
   }
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to