字符串到Leet(1337)用PHP说话

Kir*_*met 2 php string

有没有人知道我在哪里可以找到一个很好的起点来编写一个函数,它会接受一个字符串并将其转换为leet说话?

function stringToLeetSpeak($string) {
  // Logic

  return $leetString;
}
Run Code Online (Sandbox Code Playgroud)

Gum*_*mbo 6

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

$output = strtr($str, 'let', '137');
Run Code Online (Sandbox Code Playgroud)

或者str_replace与数组一起使用:

$output = str_replace(array('l','e','t'), array('1','3','7'), $str);
Run Code Online (Sandbox Code Playgroud)

有了这个,您还可以替换字符串,而不仅仅是单个字符:

$output = str_replace(array('hacker'), array('hax0r'), $str);
Run Code Online (Sandbox Code Playgroud)


Rob*_*itt 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

小用法示例:

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

希望这可以帮助.

笔记:

  • 这仅适用于个别字符,"无法转换整个单词"
  • 这是用于演示,代码可能不完美.
  • 我的建议是在PHP中研究字符串函数和数组,同时创建一个范围索引,这样你就可以组合word + char替换,使用第三个数组来存储字符串值和偏移量.