note 36877 added to function.levenshtein

From: Date: Sat, 25 Oct 2003 23:28:13 +0000
Subject: note 36877 added to function.levenshtein
Groups: php.notes 
Request: Send a blank email to php-notes+get-59291@lists.php.net to get a copy of this message
Regarding the post by fgilles on April 26th 2001, I suggest not to use levenshtein() function to test for over-uppercasing unless you've got plenty of time to waste in your host. ;) Anyhow, I think it's a useful feature, as I get really annoyed when reading whole messages in uppercase. PHP's levenshtein() function can only handle up to 255 characters, which is not realistic for user input (only the first paragraph oh this post has 285 characters). If you choose to use a custom function able to handle more than 255 characters, efficiency is an important issue. I use this function, specific for this case, but much faster: function ucase_percent ($str) { $str2 = strtolower ($str); $l = strlen ($str); $ucase = 0; for ($i = 0; $i < $l; $i++) { if ($str{$i} != $str2{$i}) { $ucase++; } } return $ucase / $l * 100.0; } I think 10% is enough for written English (maybe other languages like German, which use more capital letters, need more). With some sentencies in uppercase (everybody has the right to shout occasionally), 20% would be enough; so I use a threshold of 30%. When exceeded, I lowercase the whole message. Hope you find it useful and it helps keeping the web free of ill-mannered people. ---- Manual Page -- http://www.php.net/manual/en/function.levenshtein.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+36877 Delete -- http://master.php.net/manage/user-notes.php?action=delete+36877&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+36877&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#59291) next »