Providing a one-dimensional array of fuzzy query, only support utf-8 Unicode encoding internal processing is a special encoding format may be wrong

September 30, 2019 14:36:15

 

 
 
IF (! function_exists ( 'arrayFuzzyQuery' )) { 

    / * 
     * to provide a one-dimensional array value fuzzy query, only support utf-8 internal processing is likely to be wrong Unicode encoding a particular encoding format 
     * NOTE: This method may occur treatment time is too long problems 
     * / 

    function arrayFuzzyQuery ( $ String , Array  $ Array = [], $ Key = null ) { 

        IF ( empty ( $ String )) {
             the throw  new new \ Exception ( 'query parameters can not be empty' ); 
        } 
        IF ( empty ( $ Array )) {
             the throw  new new\ Exception ( 'Query array is empty' ); 
        } 
        $ return = []; 

        // desired similarity ratio 100% 100 
        $ expectedRatio = 50 ; 

        // also make use of the similarity comparison and contrast 
        the foreach ( $ Array  AS  $ K => $ V ) { 

            IF ( empty ( $ Key )) {
                 the similar_text ( $ String , $ V , $ Percent );
                 IF (mb_substr_count ( $ String , $ V )> 0 || $ Percent > =$expectedRatio) {
                    array_push($return, $v);
                }
            } else {
                similar_text($string, $v[$key], $percent);
                if (mb_substr_count($string, $v[$key]) > 0 || $percent >= $expectedRatio) {
                    array_push($return, $v);
                }
            }
        }
        return $return;
    }

}
 
 

 

 

 Note: similar_text to support effective little English, Chinese contrast almost 50% of this ratio needs to be adjusted based on actual business needs, through several rounds of testing, the effect can be, basic enough to take the trouble to add a mysql database to use like OR elasticsearch 

Guess you like

Origin www.cnblogs.com/zx-admin/p/11612647.html