levenshteinSynopsis
Descriptionlevenshtein() 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.
Notelevenshtein() is much faster than similar_text() ; however, similar_text() is likely to provide better results with less tweaking. NotePHP's implementation of levenshtein() cannot operate on strings longer than 255 characters in length. See also
ExampleExample 1205. Use levenshtein() to analyze how different a phrase is from its anagrams <?php $phrase = 'ACME Inc'; // Thanks to http://wordsmith.org/anagram/anagram.cgi !! $anagrams = array('In Mecca','Nice Mac','Cam Nice','Can Mice'); foreach($anagrams as $anagram) { $distance = levenshtein($phrase, $anagram); $matches[$anagram] = $distance; } natsort($matches); print_r($matches); ?> Output: Array ( [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
http://www.opencontent.org/openpub/).
The authors of this book have elected not to choose any options under the OPL. This online book was obtained
from http://www.fooassociates.com/phpfer/
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.
|