note 117555 added to function.str-rot13

From: Date: Mon, 29 Jun 2015 05:44:21 +0000
Subject: note 117555 added to function.str-rot13
Groups: php.notes 
Request: Send a blank email to php-notes+get-202985@lists.php.net to get a copy of this message
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 //================================================== STRING ROTATE EXPANDED 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; } //===== USAGE ===== 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); //===== EXAMPLES ===== $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 :-) ---- Server IP: 72.52.91.14 Probable Submitter: 2003:63:3f13:dd01:8412:2267:b4ae:120 ---- Manual Page -- http://php.net/manual/en/function.str-rot13.php Edit -- https://master.php.net/note/edit/117555 Del: integrated -- https://master.php.net/note/delete/117555/integrated Del: useless -- https://master.php.net/note/delete/117555/useless Del: bad code -- https://master.php.net/note/delete/117555/bad+code Del: spam -- https://master.php.net/note/delete/117555/spam Del: non-english -- https://master.php.net/note/delete/117555/non-english Del: in docs -- https://master.php.net/note/delete/117555/in+docs Del: other reasons-- https://master.php.net/note/delete/117555 Reject -- https://master.php.net/note/reject/117555 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202985) next »