How to check a partial similarity of two strings in PHP

前端 未结 5 1118
清歌不尽
清歌不尽 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:34

    In addition to Alex Siri's answer and according to the following article:

    http://docstore.mik.ua/orelly/webprog/php/ch04_06.htm

    PHP provides several functions that let you test whether two strings are approximately equal:

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

    SOUNDEX

    if (soundex($string1) == soundex($string2)) {
    
      echo "similar";
    
    } else {
    
      echo "not similar";
    
    }
    

    METAPHONE

    if (metaphone($string1) == metaphone($string2)) {
    
       echo "similar";
    
    } else {
    
      echo "not similar";
    
    }
    

    SIMILAR TEXT

    $similarity = similar_text($string1, $string2);
    

    LEVENSHTEIN

    $distance = levenshtein($string1, $string2); 
    

提交回复
热议问题