[人名] 利文;[地名] [英國] 利文

php levenshtein()函數(shù) 語法

作用:計算兩個字符串之間的編輯距離

語法:int?levenshtein?(?string?$str1?,?string?$str2?,?int?$cost_ins?,?int?$cost_rep?,?int?$cost_del?)

參數(shù):

參數(shù)描述
str1?求編輯距離中的其中一個字符串。
str2??求編輯距離中的另一個字符串。
cost_ins定義插入次數(shù)。
cost_rep??定義替換次數(shù)。
cost_del?定義刪除次數(shù)

說明:編輯距離,是指兩個字串之間,通過替換、插入、刪除等操作將字符串str1轉(zhuǎn)換成str2所需要操作的最少字符數(shù)量。

php levenshtein()函數(shù) 示例

<?php
$data = "hello";
$res = "world";
echo levenshtein($data,$res);
?>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

輸出:

4


<?php
$str1 = "Learning PHP";
$str2 = "is a good choise";
echo levenshtein($str1,$str2);
?>

運行實例 ?

點擊 "運行實例" 按鈕查看在線實例

輸出:

14