有没有人知道我在哪里可以找到一个很好的起点来编写一个函数,它会接受一个字符串并将其转换为leet说话?
function stringToLeetSpeak($string) {
// Logic
return $leetString;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用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)
这将是我的去处:
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)
希望这可以帮助.
笔记: