Last active
August 29, 2015 14:26
-
-
Save AnowarCST/246598a8209fac479102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Description of NameCompare | |
* | |
* @abstract String/Name compare with metaphone like soundex | |
* @return Percentage of matching with two string | |
* @description [The metaphone() function calculates the metaphone key of a string. | |
* It's more accurate than soundex() as it knows the basic rules of English pronunciation. | |
* The metaphone() function can be used for spelling applications.] | |
* @author <anowar@batworld.com> | |
* @date 04-August-2015 | |
* @link http://php.net/manual/en/function.metaphone.php | |
* @similar Similar to soundex() | |
* | |
*/ | |
class NameCompare | |
{ | |
public function NameComp($src1 = '', $src2 = '') | |
{ | |
$src1a = $this->name2ArrayMeta($src1); | |
$src2a = $this->name2ArrayMeta($src2); | |
if (count($src1a) > count($src2a)) { | |
$t = count($src1a); | |
$d = count(array_diff($src1a, $src2a)); | |
} else { | |
$t = count($src2a); | |
$d = count(array_diff($src2a, $src1a)); | |
} | |
return (($t - $d) / $t) * 100; | |
} | |
function name2ArrayMeta($name) | |
{ | |
$string1 = preg_split("/[\s,.-]+/", strtolower($name)); | |
$names = array(); | |
foreach ($string1 as $row): | |
if ($row == "md") | |
{ | |
$names[] = metaphone('mohammad'); | |
} | |
else if ($row == "mst") | |
{ | |
//$names[]=metaphone('mohammad'); | |
} | |
else if (strlen($row) > 2) | |
{ | |
if (substr($row, 0, 1) == "j") | |
$row = "z" . substr($row, 1); | |
$names[] = metaphone($row); | |
} | |
endforeach; | |
return $names; | |
} | |
} | |
$comp = new NameCompare(); | |
$string_1 = 'mohammed Anowar Hossain'; | |
$string_2 = 'muhammad Anwar Hosain'; | |
echo $string_1 . '<br/>'; | |
echo $string_2 . '<hr/>'; | |
echo $comp->NameComp($string_1, $string_2) . '% match'; //Call function with both string |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment