PHP's similar text function

Friday, June 13, 2008, 11:40 AM
Source Code by John

The similar_text function in PHP is an intriguing, if not 100% useful function. The function works like so...

$sim = similar_text($a, $b, &$p);

This delivers a variable, $p, that shows th percentage similarity between two strings.

The fundamental flaw of this function is that it has real memory performance issues when comparing medium-size strings, anywhere from 10,000 character and up depending on the amount of memory in your server. This is because the function itself, on the binary level, is built to run recursively. So, the eventual memory load is huge.

Advertisements


Oddly, if you explode your two string in PHP and run through them recursively in PHP just using similar_text to compare single word string, similar_text works very well. What I do is break my strings up into all the words, using the explode function, and then go through all the words recursively, scoring the percent similarity and the number of hits.

While you have to work around the function a bit, it is a very good function. It allows you to build scripts that will deliver quality search, with some adjustment for misspellings. I use it for a function that compares items from RSS feeds saved in a DB. This allows me to create a similar news items functions. It could also be easily integrated for a similar items function in an ecommerce shopping cart.



Mail article to a friend

© 2008 Pro Content and Design. All rights reserved.


Tools

Check Google PageRank


Recent articles

  1. 301 redirects, and the importance of keeping SERP mojo
  2. Clients chasing white whales
  3. Hitting your website with a pipe
  4. Search engine results land on some odd spots
  5. Friggin objects nested in PHP arrays
  6. PageRank from a single link over many high PR sites
  7. PlayStation 3 browser pops up in logs
  8. eBay will be the first big, successful Web 1.0 company to die
  9. It's awesome when your joke takes off
  10. Domain name generator, plus WHOIS and PageRank features

Welcome!

Wonder where to start with your web design business?

This blog follows along with my efforts to build and grow a website design business, Pro Content and Design.

The goal of this blog is to fill in blanks that may be empty as you get your business rolling.

This blog, particularly the source code section, is not intended for beginners. If you are not comfortable with databases, Ajax, DOM objects and other advanced methods, I strongly suggest you go take a look over at W3 Schools before even reading -- let alone tinkering with -- any of the code here.

I hope this blog has some value to web designers as they attempt to get their businesses going.

Good luck, and happy reading.

Thank you,
John Crawford
Pro Content and Design

Books


I highly recommend Art of the Start if you have no idea where to start with marketing.

Links

Coding
W3 Schools
IBM's Mastering Ajax Series

Graphic Design
Worth 1000
Stock.XCHNG
Urban Fonts

Website Software
Apache Web Server
SquirrelMail
PHP/Zend

Website Design Issues
Non-Standard Character Guide
Google Trends
Search Engine Optimization Analyzer

Business
Guy Kawasaki's Blog
Seth Godin's Blog
Freakonomics

Computers
NewEgg

My Main Website
Pro Content and Design

Websites I have built
PunxsyPage: local free classifieds website

Farm N Land: low-cost real estate listing website

InvestYoung: semi-defunct finance blog

Groundhog Festival: for the local summer festival

Weather Discovery Center

My Webapps
TV Stations Transmitter Database

Google PageRank Checker

Website where I did the code, database and admin
Tour de Toona: annual bicycle race in Altoona, PA