How to check a partial similarity of two strings in PHP

前端 未结 5 1116
清歌不尽
清歌不尽 2020-12-08 02:51

Is it any function in PHP that check the % of similarity of two strings?

For example i have:

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


        
5条回答
  •  有刺的猬
    2020-12-08 03:29

    As it's a nice question, I put some effort into it:

    $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;    
    }
    ?>
    

提交回复
热议问题