The PHP function implements the method of extracting a keyword from a text string,
This article describes a PHP function that implements the method of extracting keywords from a text string. Share to everyone for your reference. The specific analysis is as follows:
This is a function that locates a string as a parameter (along with other configuration optional parameters), and locates all the keywords in the string (most occurrences of the word), returning an array or a comma-delimited keyword for a string. The function works fine, but I am improving, so interested friends can make suggestions for improvement.
/** * Finds All of the keywords (words, appear most) on Param $str * and return them in order of most occurrences to Less occurrences. * @param string $str The string to search for the keywords. * @param int $minWordLen [OPTIONAL] The minimun length (number of chars) of a word to be considered a keyword. * @param int $minWordOccurrences [OPTIONAL] The minimun number of times a word have to appear * on Param $str to be conside Red a keyword. * @param boolean $asArray [optional] Specifies if the function returns a string with the * keywords separated by a comma ( $asArray = false) or a keywords array ($asArray = True). * @return mixed A string with keywords separated with commas if Param $asArray are true, * An array with the keywords othe Rwise. */function extract_keywords ($str, $minWordLen = 3, $minWordOccurrences = 2, $asArray = False) {function keyword_count_sor T ($first, $sec) {return $sec [1]-$first [1]; } $str = Preg_replace ('/[^\\w0-9]/', ' ', $str); $str = Trim (preg_repLace ('/\s+/', ', $str)); $words = Explode (' ', $str); $keywords = Array (); while (($c _word = Array_shift ($words))!== null) {if (strlen ($c _word) <= $minWordLen) continue; $c _word = strtolower ($c _word); if (array_key_exists ($c _word, $keywords)) $keywords [$c _word][1]++; else $keywords [$c _word] = Array ($c _word, 1); } usort ($keywords, ' keyword_count_sort '); $final _keywords = Array (); foreach ($keywords as $keyword _det) {if ($keyword _det[1] < $minWordOccurrences) break; Array_push ($final _keywords, $keyword _det[0]); } return $asArray? $final _keywords:implode (', ', $final _keywords);} How to Use//basic Lorem ipsum text to extract the Keywords$text = "Lorem ipsum dolor sit amet, consectetur adipiscing el It. Curabitur eget ipsum ut lorem laoreet porta a non libero. Vivamus in Tortor metus. Suspendisse Potenti. Curabitur metus nisi, adipiscing eget placerat suscipit, Suscipit vitae felis. The Integer EU odio enim, sed dignissim lorem. In Fringilla molestie Justo, Vitae varius risus lacinia ac. Nulla porttitor Justo a lectus iaculis ut vestibulum magna Egestas. Ut sed purus et nibh cursus fringilla at ID purus. "; /echoes:lorem, Suscipit, Metus, Fringilla, Purus, Justo, Eget, Vitae, Ipsum, Curabitur, Adipiscingecho extract_keywords ($ Text);
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1024920.html www.bkjia.com true http://www.bkjia.com/PHPjc/1024920.html techarticle The PHP function implements the method of extracting the keyword from a text string, and the example in this paper describes the method of extracting the keyword from a text string. Share to everyone ...