I was writing a function for my website to also rotate numbers, and kept expanding it. I added extra characters, the ability to define the shift in percent, and which type of characters to affect.



<?php

function str_shift ( $string , $perc = 50 , $useextra = FALSE , $usedigits = TRUE , $useupper = TRUE , $uselower = TRUE ) {

static $chars = array(

'lower' => 'abcdefghijklmnopqrstuvwxyz' ,

'upper' => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' ,

'digits' => '0123456789' ,

'extra' => ',.-()<>%/!"&=;:_[]{}#\\?\'+*' ,

);

settype ( $perc , 'float' );

if (! $perc ) return $string ;

$perc = fmod (( abs ( $perc ) < 1 ? 100 * $perc : $perc ), 100 );

if ( $perc < 0 ) $perc += 100 ;

$use = ( is_array ( $useextra ) ? $useextra : array( 'lower' => $uselower , 'upper' => $useupper , 'digits' => $usedigits , 'extra' => $useextra ));

foreach ( $chars as $type => $letters ) {

if (! $use [ $type ]) continue;

$shift = round ( strlen ( $letters ) * $perc / 100 );

$repl = substr ( $letters , $shift ). substr ( $letters , 0 , $shift );

$string = strtr ( $string , $letters , $repl );

}

return $string ;

}



string str_shift ( string $str [, float $percent [, bool $useextra [, bool $usedigits [, bool $uselower [, bool $useupper ] ] ] ] ] )



string str_shift ( string $str [, float $percent [, array $usetypes ] ] )



$usetypes = array( 'lower' => bool $uselower , 'upper' => bool $useupper , 'digits' => bool $usedigits , 'extra' => bool $useextra );



$string = "Peter's 17 pets (incl. 5 hamsters) love Dr Sarah Gibson, DVM!" ;

echo str_shift ( $string );

echo str_shift ( $string , 50 , TRUE );

echo str_shift ( $string , - 25 , FALSE , FALSE );

echo str_shift ( $string , 1 / 3 );

echo str_shift ( $string , 50 , array( 'lower' => TRUE , 'digits' => TRUE ));

?>



//===== RESULT =====



Crgre'f 62 crgf (vapy. 0 unzfgref) ybir Qe Fnenu Tvofba, QIZ!

Crgre"f 62 crgf [vapy: 0 unzfgref] ybir Qe Fnenu Tvofba; QIZ?

Jynyl'm 17 jynm (chwf. 5 bugmnylm) fipy Xl Mulub Acvmih, XPG!

Yncna'b 40 yncb (rwlu. 8 qjvbcnab) uxen Ma Bjajq Prkbxw, MEV!

Prgre'f 62 crgf (vapy. 0 unzfgref) ybir De Snenu Gvofba, DVM!



1. The basic parameters are the string and the percentage, 50 by default. The next four parameters allow to select which type of characters to process. There is a good reason why I put less common types first, and most obvious last. To activate extra characters you need only 1 additional parameter, otherwise you would need 4 enabling also the obvious types.



2. The definition for the letters and digits is obvious. For the extra characters I chose kind of pairs that make sense when shifted by 50%, e.g. () => [] or & => + etc. Of course you can adjust those characters to anything you like.



3. The first step is to clean the percentage, and also allow "real" floats, e.g. 1/3 for 33.33%.



4. The next step secures great flexibility when using the function. As described, instead of 4 boolean parameters you can pass 1 array. E.g. the array('upper' => TRUE) will enable the change of uppercase letters only.



5. Finally the function goes through every type and shifts the characters according to the given percentage.



I hope you can use this function and do some fancy stuff with it. Rotating characters doesn't really make sense, but it can be fun.



Arthur :-)