有谁知道在哪里可以找到一个很好的起点来编写一个函数来获取字符串并将其转换为leet说话?
function stringToLeetSpeak($string) {
// Logic
return $leetString;
}
答案 0 :(得分: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);
答案 1 :(得分: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");
希望这有帮助。
注意:
答案 2 :(得分:4)
将一个256字符串数组作为L31t表的拉丁字符。使用字符ASCII值作为数组的索引遍历字符串。必要时更换。
编辑:使用字符串捕获BoltClock的洞察力,即某些翻译需要多个字符。
答案 3 :(得分: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();
}
}