PHP比较字符串是否(几乎)相等

dir*_*irk 3 php

我需要比较可以用几种方式编写的名称.例如,圣托马斯这样的名字有时候就像圣托马斯或圣托马斯一样.最好是,我希望建立一个函数,给出比较的"平等"百分比,就像某些论坛一样(例如,这篇文章是5%编辑的).

h2o*_*ooo 20

PHP有两个(主要)内置函数.

levenshtein它计算从string1生成string2需要多少更改(删除/添加/替换).(越低越好)

similar_text返回匹配字符的数量(越高越好).请注意,您可以将引用作为第三个参数传递,它将为您提供百分比.

<?php
    $originalPost = "Here's my question to stack overflou. Thanks /h2ooooooo";
    $editedPost = "Question to stack overflow.";
    $matchingCharacters = similar_text($originalPost, $editedPost, $matchingPercentage);
    var_dump($matchingCharacters); //int(25) 
    var_dump($matchingPercentage); //float(60.975609756098) (hence edited 40%)
?>
Run Code Online (Sandbox Code Playgroud)