在给定手机号码的同一位置匹配至少5个号码位置


match at least 5 number position at same place in given mobile number

鉴于我有以下电话号码:

  • 9904773779

我想在我的数据库中搜索与上述号码至少有 5 位共同点的其他电话号码,例如:

  • 99 33723989
  • 9403 7933 7 8

我的第一个问题是使用这样的查询:

select mobileno from tbl_registration where mobileno like '%MyTextBox Value%'

但是,这并非不起作用。

我认为这里更整洁的PHP解决方案是使用similar_text

这将计算两个字符串之间的相似性。

示例演示:

$numbers = array("1234567890", "9933723989", "9403793378");
$key = "9904773779";
foreach ($numbers as $k) {           
  if (similar_text($key, $k) >= 5) { // There must be 5+ similarities
    echo $k . PHP_EOL;  
  }
}

输出:[ 9933723989, 9403793378]

查看 IDEONE 演示

我想

我会使用PHP,虽然不是很整洁,但可能有更好的方法。

<?php
$strOne = '9904773779';
$strTwo = '9933723989';
$arrOne = str_split($strOne);
$arrTwo = str_split($strTwo);
$arrIntersection = array_intersect($arrOne,$arrTwo);
$count=0;
foreach ($arrIntersection as $key => $value) {
  if ($arrOne[$key] === $arrTwo[$key]) {
    $count++;
  }
}
print_r($count);
?>

在第一阶段,我将字符串拆分为数组。然后,我使用 array_intersect 来识别重复值并将它们保存到数组中。这样就不必遍历每个数字。然后,我循环遍历相同值的数组并比较两个数组以查看值是否相同。

然而,我期待着一个更冷静的答案。