Reputation: 5283
Given that I have the following phone number:
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:
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.
Upvotes: 1
Views: 71
Reputation: 627022
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
Upvotes: 3
Reputation: 231
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.
Upvotes: 2