Skip to content

Instantly share code, notes, and snippets.

@romanitalian
Last active November 1, 2018 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save romanitalian/04541ec4b621c0b6ca76 to your computer and use it in GitHub Desktop.
Save romanitalian/04541ec4b621c0b6ca76 to your computer and use it in GitHub Desktop.
Leetify: "leet "-> "133+"; "133+" -> "leet"
<?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