soundex() - 计算字符串的soundex键 - php 字符串函数
soundex()
(PHP 4, PHP 5, PHP 7)
Calculate the soundex key of a string
说明
soundex(string $str): stringCalculates the soundex key of $str.
Soundex keys have the property that words pronounced similarly produce the same soundex key, and can thus be used to simplify searches in databases where you know the pronunciation but not the spelling. This soundex function returns a string 4 characters long, starting with a letter.
This particular soundex function is one described by Donald Knuth in "The Art Of Computer Programming, vol. 3: Sorting And Searching", Addison-Wesley(1973), pp. 391-392.
参数
$str:The input string.
返回值
Returns the soundex key as a string,或者在失败时返回FALSE
.
范例
Soundex Examples
参见
levenshtein()
计算两个字符串之间的编辑距离metaphone()
Calculate the metaphone key of a stringsimilar_text()
计算两个字符串的相似度
Since soundex() does not produce optimal results for German language we have written a function to implement the so called Kölner Phonetik (Cologne Phonetic). Please find the code below in the hope it might be useful:
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.
A MUCH easier way to check for similarity between words and avoid the problems that come up with Klancy/Clancy would be to simply add any letter infront of the string ie: OKlancy/OClancy
Although the standard soundex string is 4 characters long, and this is what's returned by the php function, some database programs return an arbitrary number of strings. MySQL, for instance. The MySQL documentation covers this, recommending that you may wish to use substring to output the standard 4 characters. Let's take 'Dostoyevski' as an example. select soundex("Dostoyevski") returns D2312 select substring(soundex("Dostoyevski"), 1, 4); returns D231 PHP will return the value as 'D231' So, to use the soundex function to generate a WHERE parameter in a MySQL SELECT statement, you might try this: $s = soundex('Dostoyevski'); SELECT * FROM authors WHERE substring(soundex(lastname), 1 , 4) = "' . $s . '"'; Or, if you want to bypass the php function $result = mysql_query("select soundex('Dostoyevski')"); $s = mysql_result($result, 0, 0);
soundex() unfortunately is very sensitive about the first character. It is not possible to use it and have Clansy and Klansy return the same value. If you want to do a phonetic search on such names you will still need to write a routine to evaluate C452 as being similar to K452.
I wrote this function a long time ago in CGI-perl and then translated (if you can call it that) into PHP. A little clunky to say the least, but should handle true soundex specs 100%: // ---begin code--- function MakeSoundEx($stringtomakesoundexof) { $temp_Name = $stringtomakesoundexof; $SoundKey1 = "BPFV"; $SoundKey2 = "CSKGJQXZ"; $SoundKey3 = "DT"; $SoundKey4 = "L"; $SoundKey5 = "MN"; $SoundKey6 = "R"; $SoundKey7 = "AEHIOUWY"; $temp_Name = strtoupper($temp_Name); $temp_Last = ""; $temp_Soundex = substr($temp_Name, 0, 1); $n = 1; for ($i = 0; $iRewritten, maybe -- but the algorithm has some obvious optimisations which can be done, for example... function text__soundex( $text ) { $k = ' 123 12 22455 12623 1 2 2'; $nl = strlen( $tN = strtoupper( $text ) ); $p = trim( $k{ ord( $tS = $tN{0} ) - 65 } ); for( $n = 1; $nI originally looked at soundex() because I wanted to compare how individual letters sounded. So, when pronouncing a string of generated characters it would be easy to to distinguish them from eachother. (ie, TGDE is hard to distinguish, whereas RFQA is easier to understand). The goal was to generate IDs that could be easily understood with a high degree of accuracy over a radio of varying quality. I quickly figured out that soundex and metaphone wouldn't do this (they work for words), so I wrote the following to help out. The ID generation function iteratively calls chrSoundAlike() to compare each new character with the preceeding characters. I'd be interested in recieving any feedback on this. Thanks.A MUCH easier way to do the above search would be to simply add any letter in front of the string and then compare them. ie. Klancy => LKlancy Clancy => LClancyadministrator at zinious dot com: Sorry but your code wasnt soundex compliant here were my results with your code, my code, and the default.. string: rest R620 perform administrator's function 0.009452 R230 perform cg's function 0.001779 R230 perform default soundex function 9.4999999999956E-005 string: reset R620 perform administrator's function 0.0055900000000001 R230 perform cg's function 0.00091799999999997 R230 perform default soundex function 0.00010600000000005 i dunno why the default, every once in a while, will for some reason be 9.xxx. very odd i think.. my code is at the bottom.. these tests were before the soundex modification as i discribe below.. btw for all the original specs on the soundex algorithm goto http://www.star-shine.net/~functionifelse/GFD/?word=soundex dalibor dot toth at podravka dot hr: yes it is perhaps sad that it gives you the same code, even metaphone has that problem.. but one might not want to be so accurate.. if somone is on search engine.. lets call it shmoogle looking for "php array reset" and search for "php array rest" then shmoogle might return stuff about beds and such.. (if they were all stupid and didnt use the first words as more important) so anyways shmoogle might need it to be less accurate in such cases.. but nonetheless.. my fix for this is to add the number of syllables at the end of the string making it 5 characters long.. this would work as fallows.. code at: http://star-shine.net/~functionifelse/cg_soundex.php or if you wanted to just use the default soundex function $str = soundex($str).cg_sylc($str); revolutionary more or less.. problly less... This function is only meant for one word though.. i'd like to see someone modify it to use split and run it through a loop to get each words cg_soundex that'll be fun ;) i would also like to sujest to the php zend apache kinda people who make php to add an optional additional variable the user can specify as fallows soundex("string",SYL); which would return the number of syllables at the end of the string highly accurate sound testing woo! also you could add VOW for vowels and CONS for consonant or whatever else someone would want.. but i really think the number of syllables will be pleanty efficiant. umm.. if this helps anyone your welcome.. ummm.. good luck in all your php adventures.. oh... and the final results syllables 1 rest 2 reset metaphone RST rest RST reset soundex R230 rest R230 reset string: rest R2301 perform cg's function 0.00211 R230 perform default soundex function 0.00011299999999997 string: reset R2302 perform cg's function 0.001691 R230 perform default soundex function 0.00010399999999999 the default function is a tad bit faster.. so maybe they will add this option and we'll have speed and accuracy. SILENT WIND OF DOOM WOOSH!Since the first letter is included in the phonetic representation in the output, it is worth pointing out that if you want a soundex key to work without the problems of klansy and clansy sounding different, take the substring from the first letter, as the first letter is the main constant of the word, and the numerical value is that of the phontic structure of the word.The soundex 'different letter in front' problem can be solved by using levenshtein() on the soundex codes. in my application, which is searching a database of album names for entries that match a particular user provided string, i do the following: 1. Search the database for the exact name 2. Search the database for entries where the name occurs anyway as a string 3. Search the database for entries where any of the words in the name (if the user has typed in more than one word) is present, except for little words (and, the, of etc) 4. Then, if all this fails, I go to plan b: - calculate the levenshtein distance (levenshtein()) between the user search term and each of the entries in the database as a percentage of the length of the user search term entered - calculate the levenshtein distance between the metphone codes of the user search term entered and each field in the database as a percentage of the length of the metaphone code of the user search term entered - calculate the levenshtein distance between the soundex codes of the user search term entered and each field in the database as a percentage of the length of the soundex code of the original user search term entered if any of these percentages is less than 50 (means that two soundex codes with different first letters will be accepted!!) then the entry is accepted as a possible match.eek... hosting got taken down on that server.. here's the code for the previous function cg_sylc($nos){ $nos = strtoupper($nos); $syllables = 0; $before = strlen($nos); $nos = str_replace(array('AA','AE','AI','AO','AU', 'EA','EE','EI','EO','EU','IA','IE','II','IO', 'IU','OA','OE','OI','OO','OU','UA','UE', 'UI','UO','UU'), "", $nos); $after = strlen($nos); $diference = $before - $after; if($before != $after) $syllables += $diference / 2; if($nos[strlen($nos)-1] == "E") $syllables --; if($nos[strlen($nos)-1] == "Y") $syllables ++; $before = $after; $nos = str_replace(array('A','E','I','O','U'),"",$nos); $after = strlen($nos); $syllables += ($before - $after); return $syllables; } function cg_SoundEx($SExStr){ $syl = cg_sylc($SExStr); $SExStr = strtoupper($SExStr); for($i = 1, $ii = 2,print $SExStr[0]; ;$ii++){ if(($SExStr[$i] != $SExStr[$ii])){ $tsstr .= $SExStr[$ii]; $i ++; } if($SExStr[$ii] == false){ break; } } $tsstr = str_replace(array('A', 'E', 'H', 'I', 'O', 'U', 'W', 'Y'), "", $tsstr); $tsstr = str_replace(array('B', 'F', 'P', 'V'), "1", $tsstr); $tsstr = str_replace(array('C', 'G', 'J', 'K', 'Q', 'S', 'X', 'Z', ''), "2", $tsstr); $tsstr = str_replace(array('D', 'T'), "3", $tsstr); $tsstr = str_replace(array('L'), "4", $tsstr); $tsstr = str_replace(array('M', 'N', ''), "5", $tsstr); $tsstr = str_replace(array('R'), "6", $tsstr); while($iiia workaround for the mysql/php differences in implementation of soundex is to do the soundex comparison entirely within mysql. for example: $sql = "SELECT * FROM table WHERE substring(soundex(field), 1, 4) = substring(soundex('".$wordsearch."'), 1, 4)";MySQL soundex (3.23.49) doesn't examine the first character at all to see whether it should be skipped. Therefore the Dutch name of The Hague, the country's government seat, 's-Gravenhage will give a soundex value of '261 in MySQL and S615 in PHP.The answer to whether soundex works except for the first letter in klancy vs clancy is to always prefix words with the same letter. aklancy will match aclancy bklancy will match bclancy soundex seems to only check the 1st 2 syllables.?? ie: spectacular matches spectacle just a thought if you rely on soundex. k-a French soundex version ; could be used for other foreigns languages where soudex lacks. Perhaps, a class with each language specifics could be writen. http://www.php-help.net/sources-php/a.french.adapted.soundex.289.htmlfie at myrealbox dot com- regarding your soudex syllable request- i think counting vowel clusters in the word will result in an accurate count of syllables. so no soudex feature is necessary, just count through the chars in the word, and everytime you run from vowel to consanant, increment the syllable count. using this logic, this sentence is categorized as follows. 2 1 2 1 1 (3) (0) (4) (0) 2 where (#) marks a word that is incorrectly categorized. i'm sure usiong a little thinking one could figure out the logic in those cases that would result in an accurate count. counting changes from vowel to consanant would yield- (1) 1 2 1 2 1 (4) 1 2 taking the average and then cieling of the two types would fix most of the errors.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!