Navigation Menu

Skip to content
This repository has been archived by the owner on Nov 14, 2017. It is now read-only.

Commit

Permalink
add Tiny class to PHP version, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kylebragger committed Oct 2, 2009
1 parent 476e63b commit 11ffd93
Showing 1 changed file with 27 additions and 22 deletions.
49 changes: 27 additions & 22 deletions tiny.php
Expand Up @@ -9,38 +9,43 @@
* Jacob DeHart (original PHP version) and Kyle Bragger (Ruby port)
*
* Usage:
* $obfuscated_id = tiny(123)
* $obfuscated_id = toTiny(123)
* $original_id = reverseTiny($obfuscated_id)
*
* Configuration:
* * You must run Tiny::generate_set() from console (using tiny.rb) to generate your $set
* Do *not* change this once you start using Tiny, as you won't be able to reverseTiny()
* any values tiny()'ed with another set.
* any values toTiny()'ed with another set.
*
*/

public function tiny($id){
$set = '__use the set you generate with tiny.rb (see tiny.rb for more)__';
class Tiny {
public static function toTiny($id){
$set = 'khvrbAfsCXEzIYt30ZdVLBQy5oJeglPHnu9M67qpKxUFj1OS8GawRTcm24DWiN';//'__use the set you generate with tiny.rb (see tiny.rb for more)__';

$HexN="";
$id = floor(abs(intval($id)));
$radix = strlen($set);
while (true) {
$R=$id%$radix;
$HexN = $set{$R}.$HexN;
$id=($id-$R)/$radix;
if ($id==0) break;
$HexN="";
$id = floor(abs(intval($id)));
$radix = strlen($set);
while (true) {
$R=$id%$radix;
$HexN = $set{$R}.$HexN;
$id=($id-$R)/$radix;
if ($id==0) break;
}
return $HexN;
}
return $HexN;
}

public function reverseTiny($str){
$set = '__use the SAME SET as tiny() above__';
$radix = strlen($set);
$strlen = strlen($str);
$N = 0;
for($i=0;$i<$strlen;$i++){
$N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1));
public static function reverseTiny($str){
$set = 'khvrbAfsCXEzIYt30ZdVLBQy5oJeglPHnu9M67qpKxUFj1OS8GawRTcm24DWiN';//$set = '__use the SAME SET as tiny() above__';
$radix = strlen($set);
$strlen = strlen($str);
$N = 0;
for($i=0;$i<$strlen;$i++){
$N += strpos($set,$str{$i})*pow($radix,($strlen-$i-1));
}
return "{$N}";
}
return "{$N}";
}

echo Tiny::toTiny(123);
echo Tiny::reverseTiny(Tiny::toTiny(123));

0 comments on commit 11ffd93

Please sign in to comment.