概念:符合可取最大公因子的两个字符串肯定满足
1:其中一个是单独的字符串 $str = ‘ABC’;
2:另一个是N倍的另一个字符串 $str2 = ‘ABCABC’;
3:如果 $str = ‘ABCD’; $str2 = ‘ABCABCABC’;这样的数据则不符合规则
class Test
{
public function num($str1, $str2)
{
//如果两个字符串相拼不相等,则不符合数据格式
if ($str1 . $str2 != $str2 . $str1) return;
// 字符串截取最大公因子 递归调用字符串长度
return substr($str1, 0, $this->diGui(strlen($str1), strlen($str2)));
}
public function diGui($s1, $s2)
{
//判断那个字符串长度长这里是$s1>$s2
if ($s1 < $s2) {
//$s2>$s1
$this->diGui($s2, $s1);
}
//如果字符串长度为0则最大公因子为另一个字符串,否则递归调用取模
return $s2 == 0 ? $s1 : $this->diGui($s2,$s1 % $s2);
}
}
$str1 = 'ABCABCABC';
$str2 = 'ABC';
$obj = new Test();
$result = $obj->num($str1, $str2);
echo $result;
输出:ABC