What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.



levenshtein — Calculates the Levenshtein distance between two strings.


int levenshtein(string_one, string_two[, insert_cost][, substitution_cost][, delete_cost]);
string string_one: First string to compare
string string_two: Second string to compare
int insert_cost (optional): Cost of inserting a character
int substitution_cost (optional): Cost of substituting a character
int delete_cost (optional): Cost of deleting a character


Integer greater than zero (0); on error, an integer less than 0


levenshtein() is used to find the Levenshtein distance between two strings. The Levenshtein distance is defined as the fewest number of insert, substitution, and delete operations needed to transform one string into another string. The function is not case-sensitive.

PHP's implementation of levenshtein() gives each operation equal weight, while many other implementations give substitution twice the cost of insertion or deletion. The cost of each operation can be defined by setting the optional insert_cost , substitution_cost , and delete_cost parameters.


levenshtein() is much faster than similar_text() ; however, similar_text() is likely to provide better results with less tweaking.


PHP's implementation of levenshtein() cannot operate on strings longer than 255 characters in length.


PHP 3.0.17+, PHP 4.0.1+

See also

To analyze the similarity of two strings:


To generate a phonetic-based key for a string:



For a very cool demo of how Levenshtein works:

Peter Kleiweg's Excellent Levenshtein Demo


Example 1205. Use levenshtein() to analyze how different a phrase is from its anagrams

$phrase = 'ACME Inc';

// Thanks to !!
$anagrams = array('In Mecca','Nice Mac','Cam Nice','Can Mice');

foreach($anagrams as $anagram) {
   $distance = levenshtein($phrase, $anagram);
   $matches[$anagram] = $distance;


    [Nice Mac] => 6
    [Cam Nice] => 6
    [Can Mice] => 6
    [In Mecca] => 7

PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at The authors of this book have elected not to choose any options under the OPL. This online book was obtained from and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.