NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - levenshtein

Trieda

Koreň \ Bez triedy

Metóda - levenshtein

(PHP 4 >= 4.0.1, PHP 5, PHP 7)

The levenshtein() function calculates the Levenshtein distance between two strings. The distance is defined as the minimal number of characters which are needed to be changed, added or removed to make both strings equal. The function determines how much the strings differ from each other. The algorithm's time complexity is O(m*n), where m and n are the lengths of the strings. The function is more effective than the similar_text() function.

Procedurálne

  • function levenshtein (string $str1, string $str2, int $cost_ins, int $cost_rep, int $cost_del) : int
  • function levenshtein (string $str1, string $str2, int $cost_ins, int $cost_rep, int $cost_del) : int

Parametre

NázovDátový typPredvolená hodnotaPopis
$str1string

The first string for the comparsion.

$str2string

The second string for the comparsion.

$cost_insint

Specifies the cost of character insertion.

$cost_repint

Specifies the cost of character replacement.

$cost_delint

Specifies the cost of character deletion.

Mávratovej hodnoty

Vracia: int

Returns Levenshtein distance between the two specified strings or -1 if one of the arguments exceeds the limit of 255 characters.

Príklady

<?php
echo levenshtein('John', 'Jog n'); // 1 insertion + 1 replacement = returns 2
echo '<br>';
echo levenshtein('John', 'Jog n', 5, 3, 1); // 1 insertion (5) + 1 replacement (3) = returns 8

Súvisiace manuály

      • function similar_text (string $first, string $second, float &$percent) : int
      Aktivity