$var1 = '9995КНМ';
$var2 = '9996КНМ';
function check(string $var1, string $var2): array
{
$matches = [];
$length = mb_strlen($var2);
for ($i = 0; $i < $length; $i++) {
for ($j = 1; $j < $length - $i + 1; $j++) {
$substr = mb_substr($var2, $i, $j);
$substrLength = mb_strlen($substr);
$substrCount = mb_substr_count($var1, $substr);
if ($substrCount) {
$matches[$substr] = [
'substr' => $substr,
'length' => $substrLength,
'count' => $substrCount,
];
}
}
}
return array_values($matches);
}
$matches = check($var1, $var2);
РезультатArray
(
[0] => Array
(
[substr] => 9
[length] => 1
[count] => 3
)
[1] => Array
(
[substr] => 99
[length] => 2
[count] => 1
)
[2] => Array
(
[substr] => 999
[length] => 3
[count] => 1
)
[3] => Array
(
[substr] => К
[length] => 1
[count] => 1
)
[4] => Array
(
[substr] => КН
[length] => 2
[count] => 1
)
[5] => Array
(
[substr] => КНМ
[length] => 3
[count] => 1
)
[6] => Array
(
[substr] => Н
[length] => 1
[count] => 1
)
[7] => Array
(
[substr] => НМ
[length] => 2
[count] => 1
)
[8] => Array
(
[substr] => М
[length] => 1
[count] => 1
)
)