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    
Last Updated Ratings Unique User Downloads Download Rankings
2019-04-23 (2 months ago) RSS 2.0 feedNot yet rated by the usersTotal: 142 This week: 2All time: 8,787 This week: 168Up
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
  Performance   Level  
Name: is available for providing paid consulting. Contact .
Classes: 50 packages by
Country: Benin Benin
Age: 29
All time rank: 8931 in Benin Benin
Week rank: 6 Up1 in Benin Benin Equal
Innovation award
Innovation award
Nominee: 22x

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 403 291 461 126 605 635 584 000 000)  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:2
All time:8,787
This week:168Up