有誰知道我在哪裏可以找到一個很好的起點來編寫一個函數,該函數需要一個字符串並將其轉換爲leet?Leet的字符串(1337)用PHP講話
function stringToLeetSpeak($string) {
// Logic
return $leetString;
}
有誰知道我在哪裏可以找到一個很好的起點來編寫一個函數,該函數需要一個字符串並將其轉換爲leet?Leet的字符串(1337)用PHP講話
function stringToLeetSpeak($string) {
// Logic
return $leetString;
}
這將是我的去處:
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");
希望這有助於。
注:
缺少:'!'=>'!!!!!!!!!!1'否則,+1 – 2010-11-06 20:38:37
你可能會有數組('a'=>'4')並添加一些排除項,所以微笑不會被疏導 – Andrew 2010-11-06 20:38:56
是的,如果他願意使用它,他可以做所有更小的事情,我剛剛複製了數組。 – RobertPitt 2010-11-06 20:40:25
製作一個256串數組作爲拉丁字符果然是高手錶。使用字符ASCII值作爲數組中的索引遍歷字符串。必要時更換。
編輯:使用字符串來捕獲BoltClock的洞察力,一些翻譯需要多個字符。
您可以使用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);
我只是改善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();
}
}
想到的l33t說話僅限於DALNET,undernet IRC頻道的喜歡,你知道那些最終在bash.org上...... – t0mm13b 2010-11-06 19:05:27