2010-11-06 45 views
2

有誰知道我在哪裏可以找到一個很好的起點來編寫一個函數,該函數需要一個字符串並將其轉換爲leet?Leet的字符串(1337)用PHP講話

function stringToLeetSpeak($string) { 
    // Logic 

    return $leetString; 
} 
+0

想到的l33t說話僅限於DALNET,undernet IRC頻道的喜歡,你知道那些最終在bash.org上...... – t0mm13b 2010-11-06 19:05:27

回答

5

這將是我的去處:

class Leetify 
{ 
    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", "|-|", "\\/\\/", "|\\/|", "|)", "\\/", "><"); 
    function encode($string) 
    { 
     $result = ''; 
     for ($i = 0; $i < strlen($string); $i++) 
     { 
      $char = $string[$i]; 

      if (false !== ($pos = array_search($char, $this->english))) 
      { 
       $char = $this->leet[$pos]; //Change the char to l33t. 
      } 
      $result .= $char; 
     } 
     return $result; 
    } 

    function decode($string) 
    { 
     //just reverse the above. 
    } 
} 

小用法示例:

$Leet = new Leet(); 
$new_leet_text = $Leet->encode("i want this text here to bee leetified xD"); 

希望這有助於。

注:

  • 這是隻是個別字符,「無法將整個單詞」
  • 這是演示和代碼可能不是完美的。
  • 我的建議是研究PHP中的字符串函數和數組,並創建一個範圍索引,以便您可以通過使用第三個數組來存儲字符串值和偏移量來組合字+字符。
+3

缺少:'!'=>'!!!!!!!!!!1'否則,+1 – 2010-11-06 20:38:37

+0

你可能會有數組('a'=>'4')並添加一些排除項,所以微笑不會被疏導 – Andrew 2010-11-06 20:38:56

+0

是的,如果他願意使用它,他可以做所有更小的事情,我剛剛複製了數組。 – RobertPitt 2010-11-06 20:40:25

4

製作一個256串數組作爲拉丁字符果然是高手錶。使用字符ASCII值作爲數組中的索引遍歷字符串。必要時更換。

編輯:使用字符串來捕獲BoltClock的洞察力,一些翻譯需要多個字符。

+0

256?做什麼的?拉丁字符具有ASCII碼中的65-90和97-122個字符。你需要50(或64;)字符數組。 – Andrew 2010-11-06 19:08:02

+0

@Andrew如果你想計算每個50-64個字符的偏移量。這是一個折中,但如果你填充整個256長度的數組,你保證沒有任何計算就有一個命中。 – 2010-11-06 20:18:36

+0

它更像C,而不是PHP-way(assoc arrays),但l33t足夠:) – Andrew 2010-11-06 20:26:08

6

您可以使用strtr翻譯某些字符:

$output = strtr($str, 'let', '137'); 

或者使用str_replace與數組:

$output = str_replace(array('l','e','t'), array('1','3','7'), $str); 

有了這個,你也可以替換字符串,而不僅僅是單個字符:

$output = str_replace(array('hacker'), array('hax0r'), $str); 
+0

5呢?或+爲tee或\ /爲v,甚至\/\ /爲w和0爲o ....:P可能更容易使用查找表... – t0mm13b 2010-11-06 19:07:32

+0

@ tommieb75:對不起,我不理解。 – Gumbo 2010-11-06 19:09:50

+1

我認爲他指的是轉換爲多字符字符串的字符,例如字母V如何轉換爲反斜槓後跟正斜槓('\ /')。 – BoltClock 2010-11-06 19:11:46

1

我只是改善Leetify(性能等)

https://gist.github.com/romanitalian/04541ec4b621c0b6ca76 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(); 
    } 
}