YouTip LogoYouTip

Func String Levenshtein

PHP levenshtein() Function |

-- Learning is not just about technology, but also about dreams!

PHP Tutorial

PHP Tutorial PHP Introduction PHP Installation PHP Syntax PHP Variables PHP echo/print PHP EOF(heredoc) PHP Data Types PHP Type Comparisons PHP Constants PHP String PHP Operators PHP If...Else PHP Switch PHP Arrays PHP Array Sorting PHP Superglobals PHP While Loop PHP For Loop PHP Functions PHP Magic Constants PHP Namespaces PHP OOP PHP Quiz

PHP Forms

PHP Forms PHP Form Validation PHP Form - Required Fields PHP Form - Validate Email and URL PHP Complete Form Example PHP $_GET Variable PHP $_POST Variable

PHP Advanced Tutorial

PHP Multidimensional Arrays PHP Date PHP Include PHP File PHP File Upload PHP Cookie PHP Session PHP E-mail PHP Secure E-mail PHP Error PHP Exception PHP Filters PHP Advanced Filters PHP JSON

PHP 7 New Features

PHP 7 New Features

PHP Database

PHP MySQL Introduction PHP MySQL Connect PHP MySQL Create Database PHP MySQL Create Table PHP MySQL Insert Data PHP MySQL Insert Multiple Data PHP MySQL Prepared Statements PHP MySQL Select Data PHP MySQL Where PHP MySQL Order By PHP MySQL Update PHP MySQL Delete PHP ODBC

PHP XML

XML Expat Parser XML DOM XML SimpleXML

PHP and AJAX

AJAX Introduction AJAX PHP AJAX Database AJAX XML AJAX Live Search AJAX RSS Reader AJAX Poll

PHP Reference Manual

PHP Array PHP Calendar PHP cURL PHP Date PHP Directory PHP Error PHP Filesystem PHP Filter PHP FTP PHP HTTP PHP Libxml PHP Mail PHP Math PHP Misc PHP MySQLi PHP PDO PHP SimpleXML PHP String PHP XML PHP Zip PHP Timezones PHP Image Processing PHP RESTful PHP PCRE PHP Available Functions PHP Composer

PHP 5 SimpleXML Functions PHP XML Functions

Explore Further

  • Math
  • Computer Science
  • Data Management
  • Programming
  • C and C++
  • Software
  • Mathematics
  • Scripting
  • Programming Languages
  • Web Services

PHP levenshtein() Function

PHP String Reference PHP String Reference

Example

Calculate the Levenshtein distance between two strings:

<?php
echo levenshtein("Hello World","ello World");
echo "<br>";
echo levenshtein("Hello World","ello World",10,20,30);
?>

Run Example Β»


Definition and Usage

The levenshtein() function returns the Levenshtein distance between two strings.

Levenshtein distance, also known as edit distance, refers to the minimum number of edit operations required to transform one string into another. The allowed edit operations include replacing a character with another, inserting a character, and deleting a character.

By default, PHP assigns the same weight to each operation (replace, insert, and delete). However, you can define the cost of each operation by setting the optional insert, replace, and delete parameters.

Note: The levenshtein() function is case-insensitive.

Note: The levenshtein() function is faster than the similar_text() function. However, the similar_text() function provides more precise results with fewer required modifications.


Syntax

levenshtein(string1, string2, insert, replace, delete)
Parameter Description
string1 Required. The first string to compare.
string2 Required. The second string to compare.
insert Optional. The cost of inserting a character. Default is 1.
replace Optional. The cost of replacing a character. Default is 1.
delete Optional. The cost of deleting a character. Default is 1.

Technical Details

Return Value: Returns the Levenshtein distance between the two parameter strings. Returns -1 if one of the strings exceeds 255 characters.
PHP Version: 4.0.1+

PHP String Reference PHP String Reference

← Func String LocaleconvFunc String Lcfirst β†’