Re: Reversing a string?

From: Date: Thu, 13 May 2004 21:35:00 +0000
Subject: Re: Reversing a string?
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-186161@lists.php.net to get a copy of this message
On Thu, 2004-05-13 at 17:27, Kristian Rasmussen wrote: > Hi all, > > I need a script for reversing a string ("hello world" becomes "dlrow > olleh"). I have tried the following: > > --- > $length = strlen($i); > $g = ""; > > while ($length > 0) > { > $g = $i[$length] . $g; > $length--; > } > echo $g; The following will work: $length = strlen( $i ); $g = ""; while( $length > 0 ) { $g .= $i{--$length}; } echo $g; But in all honesty the following is superior: $g = strrev( $i ); echo $g; Cheers, Rob. -- .------------------------------------------------------------. | InterJinn Application Framework - http://www.interjinn.com | :------------------------------------------------------------: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `------------------------------------------------------------'

« previous php.general (#186161) next »