PHP最近字符串比较
可能重复:PHP最近字符串比较,php,string-comparison,similarity,Php,String Comparison,Similarity,可能重复: 我有我的主题字符串 $subj=“董事,我的公司” 以及要比较的多个字符串的列表: $str1=“Foo-bar”$str2=“Lorem Ipsum”$str3=“董事” 我想在这里实现的是找到与$sub相关的最近字符串。可以这样做吗?您可以使用来确定两个字符串之间的距离 $subj = "Director, My Company"; $str = array(); $str[] = "Foo bar"; $str[] = "Lorem Ipsum"; $str[] = "Dir
我有我的主题字符串
$subj=“董事,我的公司”代码>
以及要比较的多个字符串的列表:
$str1=“Foo-bar”代码>
$str2=“Lorem Ipsum”代码>
$str3=“董事”代码>
我想在这里实现的是找到与$sub
相关的最近字符串。可以这样做吗?您可以使用来确定两个字符串之间的距离
$subj = "Director, My Company";
$str = array();
$str[] = "Foo bar";
$str[] = "Lorem Ipsum";
$str[] = "Director";
$minStr = "";
$minDis = PHP_INT_MAX;
for ($str as $curStr) {
$dis = levenshtein($subj, $curStr);
if ($dis < $minDis) {
$minDis = $dis;
$minStr = $curStr;
}
}
echo($minStr);
$subj=“董事,我的公司”;
$str=array();
$str[]=“Foo-bar”;
$str[]=“Lorem Ipsum”;
$str[]=“董事”;
$minStr=“”;
$minDis=PHP\u INT\u MAX;
对于($str作为$curStr){
$dis=levenshtein($subc,$curStr);
如果($dis<$minDis){
$minDis=$dis;
$minStr=$curStr;
}
}
埃科(部长);
您可以使用来确定两个字符串之间的距离
$subj = "Director, My Company";
$str = array();
$str[] = "Foo bar";
$str[] = "Lorem Ipsum";
$str[] = "Director";
$minStr = "";
$minDis = PHP_INT_MAX;
for ($str as $curStr) {
$dis = levenshtein($subj, $curStr);
if ($dis < $minDis) {
$minDis = $dis;
$minStr = $curStr;
}
}
echo($minStr);
$subj=“董事,我的公司”;
$str=array();
$str[]=“Foo-bar”;
$str[]=“Lorem Ipsum”;
$str[]=“董事”;
$minStr=“”;
$minDis=PHP\u INT\u MAX;
对于($str作为$curStr){
$dis=levenshtein($subc,$curStr);
如果($dis<$minDis){
$minDis=$dis;
$minStr=$curStr;
}
}
埃科(部长);
函数levenshtein()
将实现您所期望的功能。Levenshtein算法计算将某个字符串转换为另一个字符串所需的插入和替换操作数。结果称为编辑距离
。该距离可用于根据需要比较字符串
此示例源自PHP函数的文档
<?php
$input = 'Director, My Company';
// array of words to check against
$words = array('Foo bar','Lorem Ispum','Director');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($words as $word) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
祝你好运 levenshtein()
函数将实现您所期望的功能。Levenshtein算法计算将某个字符串转换为另一个字符串所需的插入和替换操作数。结果称为编辑距离
。该距离可用于根据需要比较字符串
此示例源自PHP函数的文档
<?php
$input = 'Director, My Company';
// array of words to check against
$words = array('Foo bar','Lorem Ispum','Director');
// no shortest distance found, yet
$shortest = -1;
// loop through words to find the closest
foreach ($words as $word) {
// calculate the distance between the input word,
// and the current word
$lev = levenshtein($input, $word);
// check for an exact match
if ($lev == 0) {
// closest word is this one (exact match)
$closest = $word;
$shortest = 0;
// break out of the loop; we've found an exact match
break;
}
// if this distance is less than the next found shortest
// distance, OR if a next shortest word has not yet been found
if ($lev <= $shortest || $shortest < 0) {
// set the closest match, and shortest distance
$closest = $word;
$shortest = $lev;
}
}
echo "Input word: $input\n";
if ($shortest == 0) {
echo "Exact match found: $closest\n";
} else {
echo "Did you mean: $closest?\n";
}
祝你好运 请定义“最近的”。你可以试试@Kagat Kagat:“最近的”是什么意思?您好,在我的示例中,最近的字符串是$str3
,它是距离$subc
最近的字符串。我不知道levenshtein()
。谢谢请定义“最近的”。你可以试试@Kagat Kagat:“最近的”是什么意思?您好,在我的示例中,最近的字符串是$str3
,它是距离$subc
最近的字符串。我不知道levenshtein()
。谢谢这与其说是回答,不如说是评论。另外,请先联系重复/类似的问题。这与其说是回答,不如说是评论。另外,请先说明重复/类似问题。