Skip to content

Commit dce4897

Browse files
authored
Merge pull request #4 from SamMousa/master
Update StringDistance.php
2 parents 8f0f91b + 76318fe commit dce4897

File tree

1 file changed

+6
-18
lines changed

1 file changed

+6
-18
lines changed

src/webd/language/StringDistance.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,27 +56,15 @@ public static function JaroWinkler($string1, $string2, $PREFIXSCALE = 0.1) {
5656
return $JaroDistance + $prefixLength * $PREFIXSCALE * (1.0 - $JaroDistance);
5757
}
5858

59-
protected static function getCommonCharacters($string1, $string2, $allowedDistance) {
59+
protected static function getCommonCharacters($string1, $string2, $allowedDistance) {
6060

61-
$str1_len = strlen($string1);
6261
$str2_len = strlen($string2);
63-
$temp_string2 = $string2;
64-
6562
$commonCharacters = '';
66-
67-
for ($i = 0; $i < $str1_len; $i++) {
68-
69-
$noMatch = True;
70-
71-
// compare if char does match inside given allowedDistance
72-
// and if it does add it to commonCharacters
73-
for ($j = max(0, $i - $allowedDistance); $noMatch && $j < min($i + $allowedDistance + 1, $str2_len); $j++) {
74-
if ($temp_string2[$j] == $string1[$i]) {
75-
$noMatch = False;
76-
77-
$commonCharacters .= $string1[$i];
78-
79-
$temp_string2[$j] = '';
63+
if (!empty($string1)) {
64+
foreach (str_split($string1) as $i => $char) {
65+
$search = strpos($string2, $char, $i <= $allowedDistance ? 0 : min($i - $allowedDistance, $str2_len));
66+
if ($search !== false && $search <= $i + $allowedDistance + 1) {
67+
$commonCharacters .= $char;
8068
}
8169
}
8270
}

0 commit comments

Comments
 (0)