PHP Classes

PHP String Rotate: Rotate strings in different ways

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 154 All time: 8,951 This week: 326Up
Version License PHP version Categories
string-rotator 1.0.1Custom (specified...5Algorithms, PHP 5, Text processing
Description Author

This class can rotate strings in different ways.

It can take a given string and rotate the position of the characters that it has in using a method that if you rotate the string again, it will return the original string.

The class implement several methods of rotation like:

- Invert the position of two characters
- Invert the position of several characters
- Rotate a string based on the current rotation factor and dictionary
- Circular rotation
- Automatically rotate using a internal map and rotation factor

Innovation Award
PHP Programming Innovation award nominee
November 2018
Number 5
String rotation is a simple way to obfuscate strings and way that it is possible to retrieve the original string from the obfuscated version.

This class provides a solution for rotating strings that implements several methods of string rotation.

Manuel Lemos
Picture of zinsou A.A.E.Mose
  Performance   Level  
Name: zinsou A.A.E.Mose is available for providing paid consulting. Contact zinsou A.A.E.Mose .
Classes: 50 packages by
Country: Benin Benin
Age: 32
All time rank: 7051 in Benin Benin
Week rank: 170 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 23x

Winner: 2x

once someone needed to know how to implement str_rot18 function with ASCII code.
My suggestion  was: 

"it is not impossible but it may be  difficult; and disrespectful of the spirit of str_rot13 which encode and also decode
I suggest to use either custom internal map to do it or implement a str_rot128 if he however prefer to use the ASCII code".

I don't know what he finally opt for , but  this give me the idea to create a class which may use dynamic map and thus dynamic rotation factor.So this package is born.

It actually allow to implement many string rotation functionalities including  the basic str_rot13 and many variants of str_rot13 and of all other allowed rotations.Of course apply rotation twice on the same string bring back the  string existing before  rotations.

Finally as bonus four static functions exist to achieve useful transformations. Only the rot method disrespect the encoding-decoding function spirit.
little explanation of why the class is useful :

Sometimes we want to implement really simple encoding function to make some text unintelligible for simple human while making the decoding process fast and as easy as the encoding process.
Str_rot13 is  a native PHP function which achieve this kindly.But it is also a really easy challenge to solve.

However solve this is really easy because the alphabet used is known by advance.Let's us now extend, with this class ,the
basic str_rot13 algorithm:

	$dictionary=str_shuffle(implode('',range('a','z	')));//create a str_rot13 custom alphabet order
	$newrotator=new str_rotator($dictionary);
	var_dump($newrotator->rotate("dictionary length must be even number and greater than 0"));// completely different from the native output
	As you may see str_rot13 is actually absolutely not the most secure way to obfuscate text but this class
	already make it a little more difficult for an human to break  easily without knowing the exact dictionary used.
	Using the same alphabet [a-z] as the native function you can thus produce 26!(factorial(26)) (about 403291461126605635584000000)  numbers of alphabet and thus the same number of variants for the str_rot13. 
This is a really basic explanation because the complexity of a solution can drastically been reduced
or augmented depending of many factors and specially if we use [a-z] or not.

You can also note that using this package a clever way could lead to really simple but relatively secure string "obfuscator" 
implementation as only you, know your dictionary length,characters order and thus the rotation factor.

list of methods:

public function __construct(string $dictionary) throws new InvalidDictionaryException when invalid dictionary is supplied
array	public function getMap() return the internal map	
int	public function getRotationFactor() return the current rotation factor
int	public function getDictionaryLength() return the current dictionary length	

void	private function init_map(array $dictionary) initialize the internal map								
string|false	public static function invertTwoCharsPos(string $string) invert characters position with something like a bubble sort except that it doesn't sort anything and that one position only  participate once.
string|false	public static function invertCharsSlot(string $string,integer $slotLength=1) invert  lots of characters position with something like a bubble sort except that it doesn't sort anything and that one slot only  participate once

string|false	public function rotate(string $string) rotate any given string string based on the current rotation factor and the given dictionary 	

string|false	public static function c_rot(string $string,integer $rotation) 	apply a kind of circular rotation on the given string
string|false	public static function autoRotate(string $string) create internal map based on the characters used in the string, choose a rotation factor based on this internal map and rotate the string according to it


  Files folder image Files  
File Role Description
Accessible without login Plain text file license.txt Lic. license file
Accessible without login Plain text file readme.txt Doc. readme
Accessible without login Plain text file RotatorTest.php Example example script
Plain text file str_rotator.class.php Class class source

 Version Control Unique User Downloads Download Rankings  
This week:0
All time:8,951
This week:326Up
For more information send a message to info at phpclasses dot org.