Skip to content

Instantly share code, notes, and snippets.

@iPublicis
Forked from romanitalian/Leetify.php
Created November 1, 2018 01:55
Show Gist options
  • Save iPublicis/5dbe469abaf9fb8d9635417541c9eb47 to your computer and use it in GitHub Desktop.
Save iPublicis/5dbe469abaf9fb8d9635417541c9eb47 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