php - 如何在 substr 中增加 80% 以上相似度的计数
php - how to increment count for above 80% similarity in substr
我正在使用以下代码来计算子字符串的出现次数。
$post= "abcdefgh abc abcd ";
$value= "abc";
$count= substr_count($post,$value);
echo $count;
但我想自定义一点,比如如果任何单词不匹配超过 80% 我不想增加计数变量,类似地如果任何单词不匹配 100% 但相似度超过 80%我想增加计数 variable.For 这我需要先获得相似度 (%)。我该怎么做?
一个选项可能是删除所有出现的搜索字符串,然后显示匹配的字符数占总字符数的比率:
$post = "abcdefgh abc abcd ";
$value = "abc";
$percent = 100.0*(strlen($post) - strlen(str_replace($value, "", $post))) / strlen($post);
echo "percent similarity: " . $percent;
如果你想知道abc
在文本中出现了多少次,我们可以尝试类似的策略:
$num_matches = (strlen($post) - strlen(str_replace($value, "", $post))) / strlen($value);
echo "number of matches: " . $num_matches;
我正在使用以下代码来计算子字符串的出现次数。
$post= "abcdefgh abc abcd ";
$value= "abc";
$count= substr_count($post,$value);
echo $count;
但我想自定义一点,比如如果任何单词不匹配超过 80% 我不想增加计数变量,类似地如果任何单词不匹配 100% 但相似度超过 80%我想增加计数 variable.For 这我需要先获得相似度 (%)。我该怎么做?
一个选项可能是删除所有出现的搜索字符串,然后显示匹配的字符数占总字符数的比率:
$post = "abcdefgh abc abcd ";
$value = "abc";
$percent = 100.0*(strlen($post) - strlen(str_replace($value, "", $post))) / strlen($post);
echo "percent similarity: " . $percent;
如果你想知道abc
在文本中出现了多少次,我们可以尝试类似的策略:
$num_matches = (strlen($post) - strlen(str_replace($value, "", $post))) / strlen($value);
echo "number of matches: " . $num_matches;