[英]Check if strings are x% equal
我想比較三個字符串:
"a text string" //target string
"a kind of similar text string"
"the cow jumps over the moon"
並設置一個百分比參數,該參數返回與目標類似的x%結果。
$results = compare($array,$target,80)
$results // Returns an array with str 1,2, because they are at least 80 similar to the target.
此外,在JavaScript或jQuery中可能有類似的功能嗎?
在PHP中,有一個函數similar_text
。 至於JavaScript,有PHP.js項目 ,它在JavaScript中重新實現PHP函數。 他們有一個你可以使用的similar_text
的實現。
似乎JavaScript實現不支持百分比參數。
您正在尋找的功能是similar_text 。
它需要一個可選的第三個參數(通過引用傳遞),其中放置百分比差異。
在您的情況下,以下應該做:
// Returns true if $str1 is at least $pct similar to $str2, otherwise false.
function compare($str1, $str2, $pct)
{
$p = 0;
similar_text($str1, $str2, $p);
return ($p >= $pct);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.