基于cesar密码的php替换编码算法
您好,我将此练习作为测试的一部分,如何解决它或提示解决它 这就是我到目前为止在encode()函数中尝试的,但它不起作用,我做错了什么基于cesar密码的php替换编码算法,php,arrays,encryption,encoding,caesar-cipher,Php,Arrays,Encryption,Encoding,Caesar Cipher,您好,我将此练习作为测试的一部分,如何解决它或提示解决它 这就是我到目前为止在encode()函数中尝试的,但它不起作用,我做错了什么 public function encode($text) { $length = strlen($text); $newstr = ''; for ($i = 0; $i < $length; $i++) { if (is_array($this->subst
public function encode($text) {
$length = strlen($text);
$newstr = '';
for ($i = 0; $i < $length; $i++) {
if (is_array($this->substitutions) && in_array(strtoupper($text[$i]), array_flip($this->substitutions)))
$newstr .= $this->substitutions[strtoupper($text[$i])];
}
return $newstr;
}
公共函数编码($text){
$length=strlen($text);
$newstr='';
对于($i=0;$i<$length;$i++){
if(is_array($this->substitutions)和&in_array(strtoupper($text[$i]),array_flip($this->substitutions)))
$newstr.=$this->substitutions[strtoupper($text[$i]);
}
返回$newstr;
}
据我所知,目前正在实施的是cesar算法,如果您能提供帮助,我们将不胜感激。您可以使用替换数组并将其拆分为两个数组,例如:
$swapA = array();
$swapB = array();
//for each item in the substitutions array take the first char
// and place in swapA and the second/last char and place in swapB
foreach($substitutions as $sub)
{
$swapA = substr($sub,0,1);
$swapB = substr($sub,1,1);
}
// the str_replace will replace the all characters in $text chars
// from position x in swapA with chars in the same position in swapB
$output = str_replace($swapA, $swapB, $text);
我的尝试-仅针对每个字符文本一个字节:
private function encodeText (string $text) : string {
$result = '';
for ($i = 0, $e = strlen ($text); $i < $e; $i ++) {
foreach ($this->substitutions as $substitution) {
$strpos = strpos ($substitution, $text {$i});
if ($strpos !== false) {
$result .= $strpos == 0 ? $substitution {1} : $substitution {0};
continue 2;
}
}
$result .= $text {$i};
}
return $result;
}
然后简单地进行翻译:
public function encode($text)
{
return strtr ($text, $this->substitutions ['from'], $this->substitutions ['to']);
}
问题是:
public function __construct(array $substitutions) {
$this->substitutions = array();
}
这是一个空数组。
改为:
public function __construct(array $substitutions) {
$this->substitutions = $substitutions;
}
然后测试你的逻辑。这里有一个更精确的答案,考虑到边缘情况
public function encode(string $text): string
{
$swapA = array();
$swapB = array();
$output = '';
$aText = str_split($text);
foreach ($this->substitutions as $sub) {
if (strlen($sub) == 2 && ($sub[0] != $sub[1])) {
$swapA[] = $sub[0];
$swapB[] = $sub[1];
} else {
throw new InvalidArgumentException ("Must have 2 different characters");
}
}
foreach ($aText as $letter) {
if (in_array(strtolower($letter), $swapA)) {
$positionOccurence = array_search($letter, $swapA);
if (strtolower($letter) != $letter) {
$replaced = strtoupper($swapB[$positionOccurence]);
} else {
$replaced = $swapB[$positionOccurence];
}
$output .= str_replace($letter, $replaced, $letter);
} elseif (in_array(strtolower($letter), $swapB)) {
$positionOccurence = array_search($letter, $swapB);
if (strtolower($letter) != $letter) {
$replaced = strtoupper($swapA[$positionOccurence]);
} else {
$replaced = $swapA[$positionOccurence];
}
$output .= str_replace($letter, $replaced, $letter);
} else {
$output .= $letter;
}
}
return $output;
}
几点提示:1。不管您的encode函数有任何其他问题,构造函数都会将
$this->substitutions
设置为空数组。2.if没有其他字符,因此如果在$this->substitutions
中找不到任何字符,则不会向$newstr
添加任何内容。是的,测试类中提供了用于根据注释测试函数的数组集,看起来$substitutions
应该是两个字符串的数组。因此,使用数组中的检查该数组中是否存在任何单个字符将始终返回false。任何解决方法都可以使用Cesar移位将所有字母按一定顺序移动,即所有字母向左移动一个。替换贴图通常不会成对交换,而是使用贴图交换。你想要实现的是一个非常特殊的成对字符交换。嗨,simcolo,只是尝试了你的解决方案,但没有解决它。replace数组变量需要在其元素中包含一对字母表originalandtheone来替换,而不需要空格
public function __construct(array $substitutions) {
$this->substitutions = array();
}
public function __construct(array $substitutions) {
$this->substitutions = $substitutions;
}
public function encode(string $text): string
{
$swapA = array();
$swapB = array();
$output = '';
$aText = str_split($text);
foreach ($this->substitutions as $sub) {
if (strlen($sub) == 2 && ($sub[0] != $sub[1])) {
$swapA[] = $sub[0];
$swapB[] = $sub[1];
} else {
throw new InvalidArgumentException ("Must have 2 different characters");
}
}
foreach ($aText as $letter) {
if (in_array(strtolower($letter), $swapA)) {
$positionOccurence = array_search($letter, $swapA);
if (strtolower($letter) != $letter) {
$replaced = strtoupper($swapB[$positionOccurence]);
} else {
$replaced = $swapB[$positionOccurence];
}
$output .= str_replace($letter, $replaced, $letter);
} elseif (in_array(strtolower($letter), $swapB)) {
$positionOccurence = array_search($letter, $swapB);
if (strtolower($letter) != $letter) {
$replaced = strtoupper($swapA[$positionOccurence]);
} else {
$replaced = $swapA[$positionOccurence];
}
$output .= str_replace($letter, $replaced, $letter);
} else {
$output .= $letter;
}
}
return $output;
}