lovephp Posted October 18, 2012 Share Posted October 18, 2012 how to get the output of this code <?php class KeywordsGenerator{ public function generateKeywords($string = '', $min_word_char = 4, $keyword_amount = 15, $exclude_words = ''){ return self::calculateKeywords($string, $min_word_char, $keyword_amount, $exclude_words); } private function calculateKeywords($string = '', $min_word_char = 3, $keyword_amount = 15, $exclude_words = '' ) { $exclude_words = explode(", ", $exclude_words); //add space before br tags so words aren't concatenated when tags are stripped $string = preg_replace('/\<br(\s*)?\/?\>/i', " ", $string); // get rid off the htmltags $string = html_entity_decode(strip_tags($string), ENT_NOQUOTES , 'UTF-8'); // count all words with str_word_count_utf8 $initial_words_array = self::str_word_count_utf8($string, 1); $total_words = sizeof($initial_words_array); $new_string = $string; //convert to lower case $new_string = mb_convert_case($new_string, MB_CASE_LOWER, "UTF-8"); // strip excluded words foreach($exclude_words as $filter_word) { $new_string = preg_replace("/\b".$filter_word."\b/i", "", $new_string); } // calculate words again without the excluded words using str_word_count_utf8 $words_array = self::str_word_count_utf8($new_string, 1); $words_array = array_filter($words_array, create_function('$var', 'return (strlen($var) >= '.$min_word_char.');')); $popularity = array(); $unique_words_array = array_unique($words_array); // create density array foreach($unique_words_array as $key => $word) { preg_match_all('/\b'.$word.'\b/i', $string, $out); $count = count($out[0]); $popularity[$key]['count'] = $count; $popularity[$key]['word'] = $word; } usort($popularity, array('MetaGenerator','cmp')); // sort array form higher to lower krsort($popularity); // create keyword array with only words $keywords = array(); foreach($popularity as $value){ $keywords[] = $value['word']; } // glue keywords to string seperated by comma, maximum 15 words $keystring = implode(', ', array_slice($keywords, 0, $keyword_amount)); // return the keywords return $keystring; } /** * Sort array by count value */ private static function cmp($a, $B) { return ($a['count'] > $b['count']) ? +1 : -1; } /** Word count for UTF8 /* Found in: http://www.php.net/%20str_word_count#85592 /* The original mask contained the apostrophe, not good for Meta keywords: /* "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}..." */ private static function str_word_count_utf8($string, $format = 0) { switch ($format) { case 1: preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches); return $matches[0]; case 2: preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches, PREG_OFFSET_CAPTURE); $result = array(); foreach ($matches[0] as $match) { $result[$match[1]] = $match[0]; } return $result; } return preg_match_all("/\p{L}[\p{L}\p{Mn}\p{Pd}]*/u", $string, $matches); } } $text = "As you probably know, having great content is only part of the equation if you want to rank well in Google and receive organic traffic. The other part is promotion and backlinks. Here are some methods you can use to promote each of your pages/posts once you publish them: * Email the URL of your page to bloggers in your niche saying they might find it interesting. And I don’t mean five or six of them. I mean email it to 100 bloggers and website owners. If you can’t find 100 in websites your niche, you aren’t trying hard enough. * Guest post on other blogs and, instead of linking to your homepage on the byline, link to the page you are trying to promote. Again, I am not talking about one or two guest posts, but ten or 20 for each page you publish targeting a long tail keyword. * Leverage social networks like Twitter and Facebook to promote the page, and perhaps create a contest to encourage people to share the page with their friends. * Post about your page on online forums, Q and A sites, social bookmarking sites, you name it. "; echo KeywordsGenerator("$text"); ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted October 18, 2012 Share Posted October 18, 2012 echo KeywordsGenerator("$text"); Put it in a variable instead of echoing it. Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 18, 2012 Share Posted October 18, 2012 It looks like it returns an array, which you can't echo anyway. Quote Link to comment Share on other sites More sharing options...
DavidAM Posted October 18, 2012 Share Posted October 18, 2012 1) Instantiate an object of the class KeywordsGenerator (it is not a function. 2) Call a method of that class (looks like it should be generateKeywords()) 3) Capture (or echo) the return value from that method call. Quote Link to comment Share on other sites More sharing options...
lovephp Posted October 18, 2012 Author Share Posted October 18, 2012 still unable guys, could show me working example? me do not got much knowledge on Class Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 18, 2012 Share Posted October 18, 2012 me do not got much knowledge on Class Seriously. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 (edited) Seriously. LOL Try this: $generator = new KeywordGenerator; echo $generator->generateKeywords($text); Edited October 18, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 18, 2012 Share Posted October 18, 2012 $generator = new KeywordGenerator; echo $generator->generateKeywords($text); It will still return an array... Quote Link to comment Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 (edited) It will still return an array... -- didn't look at that close enough. OP: $array = $generator->generateKeywords($text); // print_r or foreach through your array Edited October 18, 2012 by TOA Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 18, 2012 Share Posted October 18, 2012 OMG is that a "touche" smiley? I love it. Quote Link to comment Share on other sites More sharing options...
TOA Posted October 18, 2012 Share Posted October 18, 2012 OMG is that a "touche" smiley? I love it. Right?!? I thought I had seen one previously and I was right! They have awesome smilies on here! Quote Link to comment Share on other sites More sharing options...
lovephp Posted October 18, 2012 Author Share Posted October 18, 2012 works thanks :-) ok i got a question guys. is there any available php function which can extract keywords from a string 3 words keyword for meta tag? Quote Link to comment Share on other sites More sharing options...
requinix Posted October 18, 2012 Share Posted October 18, 2012 (edited) Uh, generateKeywords() returns calculateKeywords() which $keystring = implode(', ', array_slice($keywords, 0, $keyword_amount)); returns a string. Edited October 18, 2012 by requinix Quote Link to comment Share on other sites More sharing options...
Jessica Posted October 18, 2012 Share Posted October 18, 2012 Uh, generateKeywords() returns calculateKeywords() which $keystring = implode(', ', array_slice($keywords, 0, $keyword_amount)); returns a string. Well...uhm...then.... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.