Do you want to help your web site user find what he's looking for on your web site, even if he mis-spells a name or word in a search? PHP provides you with three facilities - soundex, metaphones and Levenshtein distance calculations - which let you compare two words and see how similar they are when written (levenshtein) or spoken (metaphone, soundex).
I've put a demonstration up for you to try -
it's here - using metaphones and levenshtein - here's the "engine" at the heart of the code:
$ident = levenshtein($first,$second);
$meta1 = metaphone($first);
$meta2 = metaphone($second);
if ($ident) {
print "Words are $ident levenshtein steps out<br>";
if ($meta1 == $meta2) {
print "But they sound the same (metaphone $meta1)\n";
} else {
$id = levenshtein($meta1,$meta2);
print "They sound different too - metaphones ";
print "$meta1 and $meta2 are $id steps out\n";
}
} else {
print "Words are identical\n";
}
The
complete source code is available too if you want to get in deep.
Having learnt how to see if two words are similar, you'll want to know how to make lots of comparisons against a single word when you're writing a search algorithm. That's another day's story perhaps, but it's something that we do as a matter of routine by keeping a database table of metaphones ....
(written 2006-03-11, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
W603 - Web and Intranet - Server Side Technologies [653] Easy feed! - (2006-03-21)
[732] Where is a web site visitor browsing from - (2006-05-24)
[1020] Parallel processing in PHP - (2007-01-03)
[1031] robots.txt - a clue to hidden pages? - (2007-01-13)
[1355] .php or .html extension? Morally Static Pages - (2007-09-17)
[1365] Korn Shell scripts on the web - (2007-09-25)
[1554] Online hotel reservations - Melksham, Wiltshire (near Bath) - (2008-02-24)
[1615] PHP training courses every month - (2008-04-18)
[1749] Using server side and client side programming together - (2008-08-11)
[2055] Effect on server when memory runs out and swapping starts - (2009-02-26)
[2282] Checking robots.txt from Python - (2009-07-12)
[3705] Django Training Courses - UK - (2012-04-23)
[3915] How does PHP work? - (2012-11-07)
[4277] Sending a message to the server and changing text on a page when a button is pressed - (2014-05-23)
Q110 - Object Orientation and General technical topics - Programming Algorithms [202] Searching for numbers - (2005-02-04)
[227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[1157] Speed Networking - a great evening and how we arranged it - (2007-04-21)
[1187] Updating a page strictly every minute (PHP, Perl) - (2007-05-14)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1949] Nuclear Physics comes to our web site - (2008-12-17)
[2189] Matching disparate referencing systems (MediaWiki, PHP, also Tcl) - (2009-05-19)
[2259] Grouping rows for a summary report - MySQL and PHP - (2009-06-27)
[2509] A life lesson from the accuracy of numbers in Excel and Lua - (2009-11-21)
[2586] And and Or illustrated by locks - (2010-01-17)
[2617] Comparing floating point numbers - a word of caution and a solution - (2010-02-01)
[2894] Sorting people by their names - (2010-07-29)
[2951] Lots of way of converting 3 letter month abbreviations to numbers - (2010-09-10)
[2993] Arrays v Lists - what is the difference, why use one or the other - (2010-10-10)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3093] How many toilet rolls - hotel inventory and useage - (2010-12-18)
[3102] AND and OR operators - what is the difference between logical and bitwise varieties? - (2010-12-24)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3620] Finding the total, average, minimum and maximum in a program - (2012-02-22)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
[4325] Learning to program - what are algorithms and design patterns? - (2014-11-22)
[4401] Selecting RECENT and POPULAR news and trends for your web site users - (2015-01-19)
[4402] Finding sum, minimum, maximum and average in Python (and Ruby) - (2015-01-19)
[4410] A good example of recursion - a real use in Python - (2015-02-01)
[4652] Testing new algorithms in PHP - (2016-02-20)
[4656] Identifying the first and last records in a sequence - (2016-02-26)
[4707] Some gems from an introduction to Python - (2016-10-29)
H107 - String Handling in PHP [31] Here documents - (2004-08-28)
[54] PHP and natural sorting - (2004-09-19)
[337] the array returned by preg_match_all - (2005-06-06)
[422] PHP Magic Quotes - (2005-08-22)
[463] Splitting the difference - (2005-10-13)
[493] Running a Perl script within a PHP page - (2005-11-12)
[558] Converting between acres and hectares - (2006-01-08)
[560] The fencepost problem - (2006-01-10)
[574] PHP - dividing a string up into pieces - (2006-01-23)
[589] Robust PHP user inputs - (2006-02-03)
[608] Don't expose your regular expressions - (2006-02-15)
[716] Evaluating arithmetic expressions in configuration files - (2006-05-10)
[728] Looking ahead and behind in a Regular Expression - (2006-05-22)
[1008] Date conversion - PHP - (2006-12-26)
[1058] PHP Regular expression to extrtact link and text - (2007-01-31)
[1195] Regular Express Primer - (2007-05-20)
[1336] Ignore case in Regular Expression - (2007-09-08)
[1372] A taster PHP expression ... - (2007-09-30)
[1533] Short and sweet and sticky - PHP form input - (2008-02-06)
[1603] Do not SHOUT and do not whisper - (2008-04-06)
[1613] Regular expression for 6 digits OR 25 digits - (2008-04-16)
[1799] Regular Expressions in PHP - (2008-09-16)
[2046] Finding variations on a surname - (2009-02-17)
[2165] Making Regular Expressions easy to read and maintain - (2009-05-10)
[2238] Handling nasty characters - Perl, PHP, Python, Tcl, Lua - (2009-06-14)
[2629] Curly braces within double quoted strings in PHP - (2010-02-09)
[3020] Handling (expanding) tabs in PHP - (2010-10-29)
[3424] Divide 10000 by 17. Do you get 588.235294117647, 588.24 or 588? - Ruby and PHP - (2011-09-08)
[3515] PHP - moving from ereg to preg for regular expressions - (2011-11-11)
[3516] Regular Expression modifiers in PHP - summary table - (2011-11-12)
[3534] Learning to program in PHP - Regular Expression and Associative Array examples - (2011-12-01)
[3788] Getting more than a yes / no answer from a regular expression pattern match - (2012-06-30)
[3789] More than just matching with a regular expression in PHP - (2012-06-30)
[3790] Solution looking for a problem? Lookahead and Lookbehind - (2012-06-30)
[4071] Setting up strings in PHP - (2013-04-27)
[4072] Splitting the difference with PHP - (2013-04-27)
Some other Articles
PHP - London course, Melksham Course, Evening courseLost CamelUsing a MySQL database from PerlIf it's Sunday, it must be BedwynHow similar are two wordsSimple but rugged form handling demoTraining Centre PicturesProgress bars and other dynamic reportsA pile of sand? Where do we stand?Carnival