Last active
November 1, 2018 01:55
-
-
Save romanitalian/04541ec4b621c0b6ca76 to your computer and use it in GitHub Desktop.
Leetify: "leet "-> "133+"; "133+" -> "leet"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// The this code migrate to github project: https://github.com/romanitalian/Leetify | |
/** | |
* Class Leetify | |
* Leetify::encode('leet'); // "133+" | |
* Leetify::decode('133+'); // "leet" | |
*/ | |
class Leetify | |
{ | |
private $string = ''; | |
private $english = array("a", "e", "s", "S", "A", "o", "O", "t", "l", "ph", "y", "H", "W", "M", "D", "V", "x"); | |
private $leet = array("4", "3", "z", "Z", "4", "0", "0", "+", "1", "f", "j", "|-|", "\\/\\/", "|\\/|", "|)", "\\/", "><"); | |
private static $inst = null; | |
private static function getInstance() { | |
if(is_null(self::$inst)) { | |
self::$inst = new self(); | |
} | |
return self::$inst; | |
} | |
private function run($isEncode = false) { | |
$out = ''; | |
if($this->string) { | |
$dict = $isEncode ? $this->english : $this->leet; | |
$dict_ = $isEncode ? $this->leet : $this->english; | |
$flippedDict = array_flip($dict); // for good performance | |
for($i = 0; $i < strlen($this->string); $i++) { | |
$char = $this->string[$i]; | |
$out .= isset($flippedDict[$char]) ? $dict_[$flippedDict[$char]] : $char; | |
} | |
} | |
return $out; | |
} | |
public function setString($string) { | |
$t = self::getInstance(); | |
$t->string = $string; | |
return $t; | |
} | |
public static function encode($string) { | |
return self::getInstance()->setString($string)->run(true); | |
} | |
public static function decode($string) { | |
return self::getInstance()->setString($string)->run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment