note 114220 added to function.soundex

From: Date: Sun, 26 Jan 2014 11:00:56 +0000
Subject: note 114220 added to function.soundex
Groups: php.notes 
Request: Send a blank email to php-notes+get-198351@lists.php.net to get a copy of this message
I made some improvements to the "Cologne Phonetic" function of niclas zimmer. Key and value of the arrays are inverted to uses simple arrays instead of multidimensional arrays. Therefore all loops and iterations are not longer necessary to find the matching value for a char. I put the function into a static class and moved the array declarations outside the function. The result is more reliable and five times faster than the original. <?php class CologneHash() { static $eLeading = array("ca" => 4, "ch" => 4, "ck" => 4, "cl" => 4, "co" => 4, "cq" => 4, "cu" => 4, "cx" => 4, "dc" => 8, "ds" => 8, "dz" => 8, "tc" => 8, "ts" => 8, "tz" => 8); static $eFollow = array("sc", "zc", "cx", "kx", "qx"); static $codingTable = array("a" => 0, "e" => 0, "i" => 0, "j" => 0, "o" => 0, "u" => 0, "y" => 0, "b" => 1, "p" => 1, "d" => 2, "t" => 2, "f" => 3, "v" => 3, "w" => 3, "c" => 4, "g" => 4, "k" => 4, "q" => 4, "x" => 48, "l" => 5, "m" => 6, "n" => 6, "r" => 7, "c" => 8, "s" => 8, "z" => 8); public static function getCologneHash($word) { if (empty($word)) return false; $len = strlen($word); for ($i = 0; $i < $len; $i++) { $value[$i] = ""; //Exceptions if ($i == 0 && $word[$i] . $word[$i + 1] == "cr") { $value[$i] = 4; } if (isset($word[$i + 1]) && isset(self::$eLeading[$word[$i] . $word[$i + 1]])) { $value[$i] = self::$eLeading[$word[$i] . $word[$i + 1]]; } if ($i != 0 && (in_array($word[$i - 1] . $word[$i], self::$eFollow))) { $value[$i] = 8; } // normal encoding if ($value[$i]=="") { if (isset(self::$codingTable[$word[$i]])) { $value[$i] = self::$codingTable[$word[$i]]; } } } // delete double values $len = count($value); for ($i = 1; $i < $len; $i++) { if ($value[$i] == $value[$i - 1]) { $value[$i] = ""; } } // delete vocals for ($i = 1; $i > $len; $i++) { // omitting first characer code and h if ($value[$i] == 0) { $value[$i] = ""; } } $value = array_filter($value); $value = implode("", $value); return $value; } } ?> ---- Server IP: 72.52.91.14 Probable Submitter: 72.52.91.14 ---- Manual Page -- http://php.net/manual/en/function.soundex.php Edit -- https://master.php.net/note/edit/114220 Del: integrated -- https://master.php.net/note/delete/114220/integrated Del: useless -- https://master.php.net/note/delete/114220/useless Del: bad code -- https://master.php.net/note/delete/114220/bad+code Del: spam -- https://master.php.net/note/delete/114220/spam Del: non-english -- https://master.php.net/note/delete/114220/non-english Del: in docs -- https://master.php.net/note/delete/114220/in+docs Del: other reasons-- https://master.php.net/note/delete/114220 Reject -- https://master.php.net/note/reject/114220 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198351) next »