简体   繁体   中英

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

Given that I have the following phone number:

  • 9904773779

I would like to search my data base for other phone numbers which have a least 5 digits in common with the above number, like those:

  • 99 33 7 2 3 98 9
  • 9 4 0 3 7 9 3 3 7 8

My first though was to use a query like this:

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

however, this didn't not work.

I think a tidier PHP solution here is using similar_text :

This calculates the similarity between two strings.

Sample demo:

$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;  
  }
}

Output: [ 9933723989, 9403793378]

See IDEONE demo

I think I would go with PHP, although not very tidy and there is probably a better way.

<?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);
?>

In the first stage I split the strings to arrays. I then use array_intersect to identify duplicate values and save them into an array. this saves having to loop through every number. I then loop thru the array of identical values and compare both arrays to see if the values are identical.

I do however look forward to a cooler answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM