function misc_get_keywords($s, $flag = 0) { $keyword_size = 3; // normalize $s = str_replace(array('. ', ',', ';', '!', '?', '"'), ' ', strtolower($s)); // remove punctuation for ($i = 0; $i < strlen($s); $i++) { if (ispunct($s[$i]) && $s[$i] != '_' && $s[$i] != '.') { $s[$i] = ' '; } } // remove eventual html tags $s = strip_tags2($s); // strtolower() // $s = strtolower ($s); // explode and trim $a = explode(' ', $s); for ($i = 0; isset($a[$i]); $i++) { $a[$i] = trim($a[$i], ' .'); } // unify $a = misc_array_unique_merge($a); // stemmer.php (english only) // if (class_exists (stemmer)) // { // $s = new stemmer; // // for ($i = 0; isset ($a[$i]); $i++) // $a[$i] = $s->stem ($a[$i]); // } $p = ''; $func = $flag ? 'isalpha' : 'isalnum'; for ($i = 0; isset($a[$i]); $i++) { $s = $a[$i]; if (strlen($s) < $keyword_size) { continue; } $found = 0; for ($j = 0; $j < strlen($s); $j++) { if (!$func($s[$j]) && $s[$j] != '_' && $s[$j] != '.') { $found = 1; break; } } if ($found == 1) { continue; } $p .= trim($s) . ' '; } // DEBUG // echo $p; return trim($p); }
function misc_get_keywords($s, $flag = 0) { $s = str_replace(array('. ', ',', ';', '!', '?', '"'), ' ', $s); $s = str_replace(array(' ', ' ', ' ', ' ', ' '), ' ', $s); for ($i = 0; $s[$i]; $i++) { if (ispunct($s[$i]) && $s[$i] != '_' && $s[$i] != '.') { $s[$i] = ' '; } } $a = explode(' ', strtolower($s)); for ($i = 0; isset($a[$i]); $i++) { $a[$i] = trim($a[$i], ' .'); } // TODO: more sensitivity instead of array_filter() $a = array_filter($a, !$flag ? 'misc_get_keywords_alnum' : 'misc_get_keywords_alpha'); $a = array_merge(array_unique($a)); // DEBUG // echo '<pre><tt>'; // print_r ($a); $s = implode(' ', $a); $s = trim($s); return $s; }