如何在PHP中检查两个字符串的部分相似性

2022-08-30 18:54:21

PHP中是否有任何函数检查两个字符串的相似性百分比?

例如,我有:

$string1="Hello how are you doing" 
$string2= " hi, how are you"

并且会把我归还真实,因为“如何”,“是”,“你”这些词都出现在行中。function($string1, $string2)

或者更好的是,返回我60%的相似性,因为“如何”,“是”,“你”是的3/5。$string1

PHP中是否存在任何功能可以做到这一点?


答案 1

由于这是一个很好的问题,我为此付出了一些努力:

<?php
$string1="Hello how are you doing";
$string2= " hi, how are you";

echo 'Compare result: ' . compareStrings($string1, $string2) . '%';
//60%


function compareStrings($s1, $s2) {
    //one is empty, so no result
    if (strlen($s1)==0 || strlen($s2)==0) {
        return 0;
    }

    //replace none alphanumeric charactors
    //i left - in case its used to combine words
    $s1clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s1);
    $s2clean = preg_replace("/[^A-Za-z0-9-]/", ' ', $s2);

    //remove double spaces
    while (strpos($s1clean, "  ")!==false) {
        $s1clean = str_replace("  ", " ", $s1clean);
    }
    while (strpos($s2clean, "  ")!==false) {
        $s2clean = str_replace("  ", " ", $s2clean);
    }

    //create arrays
    $ar1 = explode(" ",$s1clean);
    $ar2 = explode(" ",$s2clean);
    $l1 = count($ar1);
    $l2 = count($ar2);

    //flip the arrays if needed so ar1 is always largest.
    if ($l2>$l1) {
        $t = $ar2;
        $ar2 = $ar1;
        $ar1 = $t;
    }

    //flip array 2, to make the words the keys
    $ar2 = array_flip($ar2);


    $maxwords = max($l1, $l2);
    $matches = 0;

    //find matching words
    foreach($ar1 as $word) {
        if (array_key_exists($word, $ar2))
            $matches++;
    }

    return ($matches / $maxwords) * 100;    
}
?>

答案 2

正如其他答案已经说过的那样,您可以使用similar_text。演示如下:

$string1="Hello how are you doing" ;
$string2= " hi, how are you";

echo similar_text($string1, $string2, $perc); //12

echo $perc; //61.538461538462

将返回 12,并将按照您的要求设置$perc相似度的百分比。


推荐