十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
根据传入的字符串和数组,返回数组中相似度最高的字符串
创新互联是一家专业提供尤溪企业网站建设,专注与成都网站设计、做网站、HTML5、小程序制作等业务。10年已为尤溪众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。
PHP代码如下:
1.function closest_word($input, $words) {
2. $shortest = -1;
3. foreach ($words as $word) {
4. $lev = levenshtein($input, $word);
5. if ($lev == 0) {
6. $closest = $word;
7. $shortest = 0;
8. break;
9. }
10. if ($lev = $shortest || $shortest 0) {
11. $closest = $word;
12. $shortest = $lev;
13. }
14. }
15. return $closest;
16.}
2. 代码示例如下:
// 根据传入的州名(可能客户有输错),返回相似度最高的州名称
$united_state_list = array(
'AL'="Alabama",
'AK'="Alaska",
'AZ'="Arizona",
'AR'="Arkansas",
'CA'="California",
'CO'="Colorado",
'CT'="Connecticut",
'DE'="Delaware",
'DC'="District Of Columbia",
'FL'="Florida",
'GA'="Georgia",
'HI'="Hawaii",
'ID'="Idaho",
'IL'="Illinois",
'IN'="Indiana",
'IA'="Iowa",
'KS'="Kansas",
'KY'="Kentucky",
'LA'="Louisiana",
'ME'="Maine",
'MD'="Maryland",
'MA'="Massachusetts",
'MI'="Michigan",
'MN'="Minnesota",
'MS'="Mississippi",
'MO'="Missouri",
'MT'="Montana",
'NE'="Nebraska",
'NV'="Nevada",
'NH'="New Hampshire",
'NJ'="New Jersey",
'NM'="New Mexico",
'NY'="New York",
'NC'="North Carolina",
'ND'="North Dakota",
'OH'="Ohio",
'OK'="Oklahoma",
'OR'="Oregon",
'PA'="Pennsylvania",
'RI'="Rhode Island",
'SC'="South Carolina",
'SD'="South Dakota",
'TN'="Tennessee",
'TX'="Texas",
'UT'="Utah",
'VT'="Vermont",
'VA'="Virginia",
'WA'="Washington",
'WV'="West Virginia",
'WI'="Wisconsin",
'WY'="Wyoming"
);
$input_state = 'Wiscsin';
$state = closest_word($input_state ,array_values($united_state_list));
echo $state;
php自带一个函数similar_text,可以计算两个字符串的相似度,但是这个的准确性、速度不是很好。网上有很多其他的方法和现成的包,你可以搜索看看。下面简单列举一个类
class LCS {
var $str1;
var $str2;
var $c = array();
/*返回串一和串二的最长公共子序列*/
function getLCS($str1, $str2, $len1 = 0, $len2 = 0) {
$this-str1 = $str1;
$this-str2 = $str2;
if ($len1 == 0) $len1 = strlen($str1);
if ($len2 == 0) $len2 = strlen($str2);
$this-initC($len1, $len2);
return $this-printLCS($this-c, $len1 - 1, $len2 - 1);
}
/*返回两个串的相似度*/
function getSimilar($str1, $str2) {
$len1 = strlen($str1);
$len2 = strlen($str2);
$len = strlen($this-getLCS($str1, $str2, $len1, $len2));
return $len * 2 / ($len1 + $len2);
}
function initC($len1, $len2) {
for ($i = 0; $i $len1; $i++) $this-c[$i][0] = 0;
for ($j = 0; $j $len2; $j++) $this-c[0][$j] = 0;
for ($i = 1; $i $len1; $i++) {
for ($j = 1; $j $len2; $j++) {
if ($this-str1[$i] == $this-str2[$j]) {
$this-c[$i][$j] = $this-c[$i - 1][$j - 1] + 1;
} else if ($this-c[$i - 1][$j] = $this-c[$i][$j - 1]) {
$this-c[$i][$j] = $this-c[$i - 1][$j];
} else {
$this-c[$i][$j] = $this-c[$i][$j - 1];
}
}
}
}
function printLCS($c, $i, $j) {
if ($i == 0 || $j == 0) {
if ($this-str1[$i] == $this-str2[$j]) return $this-str2[$j];
else return "";
}
if ($this-str1[$i] == $this-str2[$j]) {
return $this-printLCS($this-c, $i - 1, $j - 1).$this-str2[$j];
} else if ($this-c[$i - 1][$j] = $this-c[$i][$j - 1]) {
return $this-printLCS($this-c, $i - 1, $j);
} else {
return $this-printLCS($this-c, $i, $j - 1);
}
}
}
重新排序呗
sort($arry) {
$id = array_search(6, $arry);// 获取键值
echo $arry[($id+1)]; //获取最相近的值
}
array_intersect 函数可以返回多个数组中相同的数据,如果你不要相同只要相似,那你就得自己写个foreach嵌套foreach循环比较了。